Skip to content

Commit c79e499

Browse files
committed
Apply lifetimebound to the Option API
1 parent 7b8c55b commit c79e499

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

subspace/option/option.h

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "subspace/iter/from_iterator.h"
3232
#include "subspace/iter/into_iterator.h"
3333
#include "subspace/macros/always_inline.h"
34-
#include "subspace/macros/compiler.h"
34+
#include "subspace/macros/lifetimebound.h"
3535
#include "subspace/macros/nonnull.h"
3636
#include "subspace/marker/unsafe.h"
3737
#include "subspace/mem/clone.h"
@@ -129,7 +129,7 @@ class Option final {
129129
}
130130
}
131131

132-
static inline constexpr Option some(T& t) noexcept
132+
static inline constexpr Option some(T& t sus_lifetimebound) noexcept
133133
requires(std::is_reference_v<T>)
134134
{
135135
return Option(move_to_storage(t));
@@ -385,12 +385,14 @@ class Option final {
385385
/// reuse, making variable names bad.
386386
/// * It's expected due to std::optional and general container-of-one things
387387
/// to provide access through operator* and operator->.
388-
constexpr const std::remove_reference_t<T>& operator*() const& noexcept {
388+
constexpr const std::remove_reference_t<T>& operator*() const& noexcept
389+
sus_lifetimebound {
389390
::sus::check(t_.state() == Some);
390391
return t_.val();
391392
}
392393
constexpr const std::remove_reference_t<T>* operator*() && noexcept = delete;
393-
constexpr std::remove_reference_t<T>& operator*() & noexcept {
394+
constexpr std::remove_reference_t<T>& operator*() & noexcept
395+
sus_lifetimebound {
394396
::sus::check(t_.state() == Some);
395397
return t_.val_mut();
396398
}
@@ -420,13 +422,15 @@ class Option final {
420422
/// reuse, making variable names bad.
421423
/// * It's expected due to std::optional and general container-of-one things
422424
/// to provide access through operator* and operator->.
423-
constexpr const std::remove_reference_t<T>* operator->() const& noexcept {
425+
constexpr const std::remove_reference_t<T>* operator->() const& noexcept
426+
sus_lifetimebound {
424427
::sus::check(t_.state() == Some);
425428
return ::sus::mem::addressof(
426429
static_cast<const std::remove_reference_t<T>&>(t_.val()));
427430
}
428431
constexpr const std::remove_reference_t<T>* operator->() && noexcept;
429-
constexpr std::remove_reference_t<T>* operator->() & noexcept {
432+
constexpr std::remove_reference_t<T>* operator->() & noexcept
433+
sus_lifetimebound {
430434
::sus::check(t_.state() == Some);
431435
return ::sus::mem::addressof(
432436
static_cast<std::remove_reference_t<T>&>(t_.val_mut()));
@@ -486,7 +490,7 @@ class Option final {
486490
///
487491
/// If it is non-trivial to construct `T`, the <get_or_insert_with>() method
488492
/// would be preferable, as it only constructs a `T` if needed.
489-
T& get_or_insert(T t) & noexcept
493+
T& get_or_insert(T t) & noexcept sus_lifetimebound
490494
requires(sus::mem::MoveOrRef<T>)
491495
{
492496
if (t_.state() == None) {
@@ -507,7 +511,7 @@ class Option final {
507511
///
508512
/// The Option's contained type `T` must be #Default, and will be
509513
/// constructed through that trait.
510-
constexpr T& get_or_insert_default() & noexcept
514+
constexpr T& get_or_insert_default() & noexcept sus_lifetimebound
511515
requires(::sus::construct::Default<T>)
512516
{
513517
if (t_.state() == None) t_.construct_from_none(T());
@@ -835,7 +839,8 @@ class Option final {
835839

836840
/// Returns an Option<const T&> from this Option<T>, that either holds #None
837841
/// or a reference to the value in this Option.
838-
constexpr Option<const std::remove_reference_t<T>&> as_ref() const& noexcept {
842+
constexpr Option<const std::remove_reference_t<T>&> as_ref() const& noexcept
843+
sus_lifetimebound {
839844
if (t_.state() == None)
840845
return Option<const std::remove_reference_t<T>&>::none();
841846
else
@@ -855,7 +860,7 @@ class Option final {
855860

856861
/// Returns an Option<T&> from this Option<T>, that either holds #None or a
857862
/// reference to the value in this Option.
858-
constexpr Option<T&> as_mut() & noexcept {
863+
constexpr Option<T&> as_mut() & noexcept sus_lifetimebound {
859864
if (t_.state() == None)
860865
return Option<T&>::none();
861866
else
@@ -873,7 +878,8 @@ class Option final {
873878
return Option<T&>(t_.take_and_set_none());
874879
}
875880

876-
constexpr Once<const std::remove_reference_t<T>&> iter() const& noexcept {
881+
constexpr Once<const std::remove_reference_t<T>&> iter() const& noexcept
882+
sus_lifetimebound {
877883
return Once<const std::remove_reference_t<T>&>::with(as_ref());
878884
}
879885
constexpr Once<const std::remove_reference_t<T>&> iter() && noexcept
@@ -883,7 +889,9 @@ class Option final {
883889
::sus::move(*this).as_ref());
884890
}
885891

886-
constexpr Once<T&> iter_mut() & noexcept { return Once<T&>::with(as_mut()); }
892+
constexpr Once<T&> iter_mut() & noexcept sus_lifetimebound {
893+
return Once<T&>::with(as_mut());
894+
}
887895
constexpr Once<T&> iter_mut() && noexcept
888896
requires(std::is_reference_v<T>)
889897
{
@@ -1048,7 +1056,7 @@ using sus::iter::__private::end;
10481056
/// Option<T&> correctly.
10491057
template <class T>
10501058
[[nodiscard]] inline constexpr __private::SomeMarker<T&&> some(
1051-
T&& t sus_if_clang([[clang::lifetimebound]])) noexcept {
1059+
T&& t sus_lifetimebound) noexcept {
10521060
return __private::SomeMarker<T&&>(::sus::forward<T>(t));
10531061
}
10541062

0 commit comments

Comments
 (0)