Class Subspace :: sus :: option :: Option

template <class T>
class Option final
{ ... };

The Option type.

See the namespace level documentation for more.

Static Methods

Default-construct an option that is holding no value.

This satisfies Default for Option.

Option(const T& t)
requires
!std::is_reference_v<T>
sus::mem::Copy<T>
template <class U>
Option(U&& u)
requires
std::convertible_to<U, T>
!std::is_reference_v<T>
sus::mem::Move<T>
sus::mem::IsMoveRef<decltype(u)>
template <class U>
Option(U&& t)
requires
std::convertible_to<U, T>
std::is_reference_v<T>
sus::construct::SafelyConstructibleFromReference<T, U &&>

Construct an option that is holding the given value.

Const References

For Option<const T&> it is possible to bind to a temporary which would create a memory safety bug. The [[clang::lifetimebound]] attribute is used to prevent this via Clang. But additionally, the incoming type is required to match with SafelyConstructibleFromReference to prevent conversions that would construct a temporary.

To force accepting a const reference anyway in cases where a type can convert to a reference without constructing a temporary, use an unsafe static_cast<const T&>() at the callsite and document why a temporary is not constructed.

template <class U>
Option(const Option<U>& other)
requires
std::convertible_to<U, T>
std::is_reference_v<U> == std::is_reference_v<T>
template <class U>
Option(Option<U>&& other)
requires
std::convertible_to<U &&, T>
std::is_reference_v<U> == std::is_reference_v<T>
template <class U>
Option(Option<U>&& other)
requires
!std::is_reference_v<U>
std::is_reference_v<T>
deleted

Converts from Option<X> to Option<Y> if X is convertible to Y.

Option(const Option<T>& o)
requires
sus::mem::CopyOrRef<T>
Option(const Option<T>& o)
requires
!::sus::mem::CopyOrRef<T>
deleted

Copy constructor for Option<T> which will satisfy Copy<Option<T>> if Copy<T> is satisfied.

If T can be trivially copy-constructed, then Option<T> can also be trivially copy-constructed.

Option(Option<T>&& o)
requires
sus::mem::MoveOrRef<T>
Option(Option<T>&& o)
requires
!::sus::mem::MoveOrRef<T>
deleted

Move constructor for Option<T> which will satisfy Move<Option<T>> if Move<T> is satisfied.

If T can be trivially move-constructed, then Option<T> can also be trivially move-constructed. When trivially-moved, the option is copied on move, and the moved-from Option is unchanged but should still not be used thereafter without reinitializing it. Use take() instead to move the value out of the option when the option may be used again afterward.

template <class U>
Option(std::optional<U>&& s)
requires
std::same_as<U, T>
!std::is_reference_v<T>
template <class U>
Option(const std::optional<U>& s)
requires
std::same_as<U, T>
!std::is_reference_v<T>

Implicit conversion from std::optional.

template <class U>
static auto from(U&& val) -> Option<T>
requires
std::constructible_from<Option<T>, U &&>
!std::is_reference_v<T>
template <class U>
static auto from(U&& val) -> Option<T>
requires
std::constructible_from<Option<T>, U &&>
std::is_reference_v<T>

Moves or copies val into a new option holding Some(val).

Implements From<Option<T>, T>.

template <class Iter>
static auto from_product(Iter&& it) -> Option<T>
requires
sus::iter::Iterator<Iter, Option<T>>
sus::iter::Product<T>

Computes the product of an iterator over Option<T> as long as there is no None found. If a None is found, the function returns None.

Prefer to call product() on the iterator rather than calling from_product() directly.

Implements sus::iter::Product<Option<T>>.

The product is computed using the implementation of the inner type T which also satisfies sus::iter::Product<T>.

template <class Iter>
static auto from_sum(Iter&& it) -> Option<T>
requires
sus::iter::Iterator<Iter, Option<T>>
sus::iter::Sum<T>

Computes the sum of an iterator over Option<T> as long as there is no None found. If a None is found, the function returns None.

Prefer to call sum() on the iterator rather than calling from_sum() directly.

Implements sus::iter::Sum<Option<T>>.

The sum is computed using the implementation of the inner type T which also satisfies sus::iter::Sum<T>.

Methods

template <class U>
auto and_that(Option<U> that) && -> Option<U>
template <class U>
auto and_that(Option<U> that) const& -> Option<U>
requires
sus::mem::CopyOrRef<T>

Consumes this option and returns an option with None if this option holds None, otherwise returns that option.

template <class AndFn>
auto and_then(AndFn f) && -> sus::fn::ReturnOnce<AndFn, T&&>
requires
sus::fn::FnOnce<AndFn, ::sus::fn::NonVoid (T &&)>
__private::IsOptionType<sus::fn::ReturnOnce<AndFn, T &&>>::value
template <class AndFn>
auto and_then(AndFn f) const& -> sus::fn::ReturnOnce<AndFn, T&&>
requires
sus::fn::FnOnce<AndFn, ::sus::fn::NonVoid (T &&)>
__private::IsOptionType<sus::fn::ReturnOnce<AndFn, T &&>>::value
sus::mem::CopyOrRef<T>

Consumes this option and returns an option with None if this option holds None, otherwise calls f with the contained value and returns the result.

The function f receives the option's inner T and can return any Option<U>.

Some languages call this operation flatmap.

auto as_mut() & -> Option<T&>
auto as_mut() && -> Option<T&>
requires
std::is_reference_v<T>
auto as_mut() const& -> Option<T&>
requires
std::is_reference_v<T>

Returns an Option<T&> from this Option<T>, that either holds None or a reference to the value in this option.

auto as_ref() const& -> Option<const std::remove_reference_t<T>&>
auto as_ref() && -> Option<const std::remove_reference_t<T>&>
requires
std::is_reference_v<T>

Returns an Option<const T&> from this Option<T>, that either holds None or a reference to the value in this option.

Implementation Notes

Implementation note: We only allow calling this on an rvalue option if the contained value is a reference, otherwise we are returning a reference to a short-lived object which leads to common C++ memory bugs.

auto as_value() const& -> const std::remove_reference_t<T>&
auto as_value() && -> const std::remove_reference_t<T>&
requires
std::is_reference_v<T>

Returns a const reference to the contained value inside the option.

To extract the value inside an option, use unwrap on an rvalue, and take to move the contents of an lvalue option to an rvalue.

Panics

The function will panic without a message if the option's state is currently None.

Implementation Notes

Implementation note: We only allow calling this on an rvalue Option if the contained value is a reference, otherwise we are returning a reference to a short-lived object which leads to common C++ memory bugs.

auto as_value_mut() & -> std::remove_reference_t<T>&
auto as_value_mut() && -> std::remove_reference_t<T>&
requires
std::is_reference_v<T>
auto as_value_mut() const& -> std::remove_reference_t<T>&
requires
std::is_reference_v<T>

Returns a mutable reference to the contained value inside the option.

To extract the value inside an option, use unwrap on an rvalue, and take to move the contents of an lvalue option to an rvalue.

Panics

The function will panic without a message if the option's state is currently None.

Implementation Notes

Implementation note: We only allow calling this on an rvalue Option if the contained value is a reference, otherwise we are returning a reference to a short-lived object which leads to common C++ memory bugs.

auto as_value_unchecked(UnsafeFnMarker) const& -> const std::remove_reference_t<T>&
auto as_value_unchecked(UnsafeFnMarker) && -> const std::remove_reference_t<T>&
requires
std::is_reference_v<T>

Returns a const reference to the contained value inside the option.

To extract the value inside an option, use unwrap_unchecked on an rvalue, and take to move the contents of an lvalue option to an rvalue.

Safety

The option's state must be Some or Undefined Behaviour results.

Implementation Notes

Implementation note: We only allow calling this on an rvalue Option if the contained value is a reference, otherwise we are returning a reference to a short-lived object which leads to common C++ memory bugs.

auto as_value_unchecked_mut(UnsafeFnMarker) & -> std::remove_reference_t<T>&
auto as_value_unchecked_mut(UnsafeFnMarker) && -> std::remove_reference_t<T>&
requires
std::is_reference_v<T>
auto as_value_unchecked_mut(UnsafeFnMarker) const& -> std::remove_reference_t<T>&
requires
std::is_reference_v<T>

Returns a mutable reference to the contained value inside the option.

To extract the value inside an option, use unwrap_unchecked on an rvalue, and take to move the contents of an lvalue option to an rvalue.

Safety

The option's state must be Some or Undefined Behaviour results.

Implementation Notes

Implementation note: We only allow calling this on an rvalue Option if the contained value is a reference, otherwise we are returning a reference to a short-lived object which leads to common C++ memory bugs.

auto clone() const& -> Option<T>
requires
sus::mem::Clone<T>
!::sus::mem::CopyOrRef<T>

Satisifies the Clone concept when Option is not Copy.

auto clone_from(const Option<T>& source) & -> void
requires
sus::mem::Clone<T>
!::sus::mem::CopyOrRef<T>

Satisifies the CloneFrom concept.

auto cloned() const& -> Option<std::remove_const_t<std::remove_reference_t<T>>>
requires
std::is_reference_v<T>
sus::mem::Clone<T>

Maps an Option<T&> to an Option<T> by cloning the referenced T.

auto copied() const& -> Option<std::remove_const_t<std::remove_reference_t<T>>>
requires
std::is_reference_v<T>
sus::mem::Copy<T>

Maps an Option<T&> to an Option<T> by copying the referenced T.

auto expect(const char* message) && -> T
auto expect(const char* message) const& -> T
requires
sus::mem::CopyOrRef<T>

Returns the contained value inside the option.

The function will panic with the given message if the option's state is currently None.

auto filter(FnOnce<bool(const std::remove_reference_t<T>&)> auto p) && -> Option<T>
auto filter(FnOnce<bool(const std::remove_reference_t<T>&)> auto p) const& -> Option<T>
requires
sus::mem::CopyOrRef<T>

Consumes the option and applies a predicate function to the value contained in the option. Returns a new option with the same value if the predicate returns true, otherwise returns an empty Option.

The predicate function must be able to receive const T& and return a value that converts tobool.

auto flatten() && -> T
requires
::sus::option::__private::IsOptionType<T>::value
auto flatten() const& -> T
requires
::sus::option::__private::IsOptionType<T>::value
sus::mem::CopyOrRef<T>

Maps an Option<Option<T>> to an Option<T>.

auto get_or_insert(T value) & -> T&
requires
!std::is_reference_v<T>
sus::mem::Move<T>
template <class U>
auto get_or_insert(U&& value) & -> T&
requires
std::convertible_to<U, T>
std::is_reference_v<T>
sus::construct::SafelyConstructibleFromReference<T, U &&>

If the option holds a value, returns a mutable reference to it. Otherwise, stores value inside the option and returns a mutable reference to it.

If it is non-trivial to construct T, the Option::get_or_insert_with method would be preferable, as it only constructs a T if needed.

auto get_or_insert_default() & -> T&
requires
sus::construct::Default<T>

If the option holds a value, returns a mutable reference to it. Otherwise, constructs a default value T, stores it inside the option and returns a mutable reference to the new value.

This method differs from unwrap_or_default in that it does not consume the option, and instead it can not be called on rvalues.

This is a shorthand for Option<T>::get_or_insert_with([] { return T(); }).

The option's contained type T must satisfy Default so it can be constructed with its default value.

auto get_or_insert_with(FnOnce<T()> auto f) & -> T&

If the option holds a value, returns a mutable reference to it. Otherwise, constructs a T by calling f, stores it inside the option and returns a mutable reference to the new value.

This method differs from unwrap_or_else in that it does not consume the option, and instead it can not be called on rvalues.

auto insert(T value) & -> T&
requires
!std::is_reference_v<T>
sus::mem::Move<T>
template <class U>
auto insert(U&& value) & -> T&
requires
std::convertible_to<U, T>
std::is_reference_v<T>
sus::construct::SafelyConstructibleFromReference<T, U &&>

Inserts value into the option, then returns a mutable reference to it.

If the option already contains a value, the old value is dropped.

See also Option::get_or_insert, which doesn’t update the value if the option already contains Some.

auto into_iter() && -> sus::option::OptionIter<T>
auto into_iter() const& -> sus::option::OptionIter<T>
requires
sus::mem::CopyOrRef<T>

Produces an Iterator over the single item in the Option, or an empty iterator. If the option holds a value, the iterator will return ownership of the value. If the option holds a reference, it will return that reference.

auto is_none() const -> bool

Returns whether the option is currently empty, containing no value.

auto is_some() const -> bool

Returns whether the option currently contains a value.

If there is a value present, it can be extracted with unwrap or expect. For lvalues, it can be accessed as a reference through as_value and as_value_mut for explicit const/mutable access, or through operator* and operator->.

auto iter() const& -> sus::option::OptionIter<const std::remove_reference_t<T>&>
auto iter() && -> sus::option::OptionIter<const std::remove_reference_t<T>&>
requires
std::is_reference_v<T>
auto iter() const& -> sus::option::OptionIter<const std::remove_reference_t<T>&>
requires
std::is_reference_v<T>

Produces an Iterator over the single item in the Option, or an empty iterator. The iterator will return a const reference.

auto iter_mut() & -> sus::option::OptionIter<T&>
auto iter_mut() && -> sus::option::OptionIter<T&>
requires
std::is_reference_v<T>
auto iter_mut() const& -> sus::option::OptionIter<T&>
requires
std::is_reference_v<T>

Produces an Iterator over the single item in the Option, or an empty iterator. If the Option holds a value, the iterator will return a mutable reference to it. If the Option holds a reference, it will return that reference.

template <class MapFn>
auto map(MapFn m) && -> Option<sus::fn::ReturnOnce<MapFn, T&&>>
requires
sus::fn::FnOnce<MapFn, ::sus::fn::NonVoid (T &&)>
template <class MapFn>
auto map(MapFn m) const& -> Option<sus::fn::ReturnOnce<MapFn, T&&>>
requires
sus::fn::FnOnce<MapFn, ::sus::fn::NonVoid (T &&)>
sus::mem::CopyOrRef<T>

Maps the option's value through a function.

When called on an rvalue, it consumes the option, passing the value through the map function, and returning an Option<R> where R is the return type of the map function.

Returns an Option<R> in state None if the current option is in state None.

template <class MapFn>
auto map_or(sus::fn::ReturnOnce<MapFn, T&&> default_result, MapFn m) && -> sus::fn::ReturnOnce<MapFn, T&&>
requires
sus::fn::FnOnce<MapFn, ::sus::fn::NonVoid (T &&)>
template <class MapFn>
auto map_or(sus::fn::ReturnOnce<MapFn, T&&> default_result, MapFn m) const& -> sus::fn::ReturnOnce<MapFn, T&&>
requires
sus::fn::FnOnce<MapFn, ::sus::fn::NonVoid (T &&)>
sus::mem::CopyOrRef<T>

Returns the provided default result (if none), or applies a function to the contained value (if any).

Arguments passed to map_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use map_or_else, which is lazily evaluated.

template <class DefaultFn, class MapFn>
auto map_or_else(DefaultFn default_fn, MapFn m) && -> sus::fn::ReturnOnce<DefaultFn>
requires
sus::fn::FnOnce<DefaultFn, ::sus::fn::NonVoid (void)>
sus::fn::FnOnce<MapFn, ::sus::fn::ReturnOnce<DefaultFn> (T &&)>
template <class DefaultFn, class MapFn>
auto map_or_else(DefaultFn default_fn, MapFn m) const& -> sus::fn::ReturnOnce<DefaultFn>
requires
sus::fn::FnOnce<DefaultFn, ::sus::fn::NonVoid (void)>
sus::fn::FnOnce<MapFn, ::sus::fn::ReturnOnce<DefaultFn> (T &&)>
sus::mem::CopyOrRef<T>

Computes a default function result (if none), or applies a different function to the contained value (if any).

template <class E, int &... , class Result = ::sus::result::Result<T, E>>
auto ok_or(E e) && -> Result
requires
!std::is_reference_v<T>
!std::is_reference_v<E>
template <class E, int &... , class Result = ::sus::result::Result<T, E>>
auto ok_or(E e) const& -> Result
requires
!std::is_reference_v<T>
!std::is_reference_v<E>
sus::mem::CopyOrRef<T>

Transforms the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(e).

Arguments passed to ok_or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use ok_or_else, which is lazily evaluated.

template <class ElseFn>
auto ok_or_else(ElseFn f) && -> sus::result::Result<T, sus::fn::ReturnOnce<ElseFn>>
requires
sus::fn::FnOnce<ElseFn, ::sus::fn::NonVoid (void)>
!std::is_reference_v<T>
!std::is_reference_v<sus::fn::ReturnOnce<ElseFn>>
template <class ElseFn>
auto ok_or_else(ElseFn f) const& -> sus::result::Result<T, sus::fn::ReturnOnce<ElseFn>>
requires
sus::fn::FnOnce<ElseFn, ::sus::fn::NonVoid (void)>
!std::is_reference_v<T>
!std::is_reference_v<sus::fn::ReturnOnce<ElseFn>>
sus::mem::CopyOrRef<T>

Transforms the Option<T> into a Result<T, E>, mapping Some(v) to Ok(v) and None to Err(f()).

auto or_else(FnOnce<Option<T>()> auto f) && -> Option<T>
auto or_else(FnOnce<Option<T>()> auto f) const& -> Option<T>
requires
sus::mem::CopyOrRef<T>

Consumes and returns an option with the same value if this option contains a value, otherwise returns the option returned by f.

auto or_that(Option<T> that) && -> Option<T>
auto or_that(Option<T> that) const& -> Option<T>
requires
sus::mem::CopyOrRef<T>

Consumes and returns an option with the same value if this option contains a value, otherwise returns that option.

auto replace(T value) & -> Option<T>
requires
!std::is_reference_v<T>
sus::mem::Move<T>
template <class U>
auto replace(U&& value) & -> Option<T>
requires
std::convertible_to<U, T>
std::is_reference_v<T>
sus::construct::SafelyConstructibleFromReference<T, U &&>

Replaces whatever the option is currently holding with value and returns an Option holding what was there previously, which may be empty.

auto take() & -> Option<T>

Returns a new option containing whatever was inside the current option.

If this option contains None then it is left unchanged and returns an option containing None. If this option contains Some with a value, the value is moved into the returned option and this option will contain None afterward.

template <int &... , class OkType = typename ::sus::result::__private::IsResultType<T>::ok_type, class ErrType = typename ::sus::result::__private::IsResultType<T>::err_type, class Result = ::sus::result::Result<Option<OkType>, ErrType>>
auto transpose() && -> Result
requires
::sus::result::__private::IsResultType<T>::value
template <int &... , class OkType = typename ::sus::result::__private::IsResultType<T>::ok_type, class ErrType = typename ::sus::result::__private::IsResultType<T>::err_type, class Result = ::sus::result::Result<Option<OkType>, ErrType>>
auto transpose() const& -> Result
requires
::sus::result::__private::IsResultType<T>::value
sus::mem::CopyOrRef<T>

Transposes an Option of a Result into a Result of an Option.

None will be mapped to Ok(None). Some(Ok(_)) and Some(Err(_)) will be mapped to Ok(Some(_)) and Err(_).

auto unwrap() && -> T
auto unwrap() const& -> T
requires
sus::mem::CopyOrRef<T>

Returns the contained value inside the option.

The function will panic without a message if the option's state is currently None.

auto unwrap_or(T default_result) && -> T
auto unwrap_or(T default_result) const& -> T
requires
sus::mem::CopyOrRef<T>

Returns the contained value inside the option, if there is one. Otherwise, returns default_result.

Note that if it is non-trivial to construct a default_result, that unwrap_or_else should be used instead, as it will only construct the default value if required.

auto unwrap_or_default() && -> T
requires
!std::is_reference_v<T>
sus::construct::Default<T>
auto unwrap_or_default() const& -> T
requires
!std::is_reference_v<T>
sus::construct::Default<T>
sus::mem::Copy<T>

Returns the contained value inside the option, if there is one. Otherwise, constructs a default value for the type and returns that.

The option's contained type T must be Default in order to be constructed with a default value.

auto unwrap_or_else(FnOnce<T()> auto f) && -> T
auto unwrap_or_else(FnOnce<T()> auto f) const& -> T
requires
sus::mem::CopyOrRef<T>

Returns the contained value inside the option, if there is one. Otherwise, returns the result of the given function.

auto unwrap_unchecked(UnsafeFnMarker) const& -> T
requires
sus::mem::CopyOrRef<T>

Returns the contained value inside the option.

Safety

It is Undefined Behaviour to call this function when the option's state is None. The caller is responsible for ensuring the option contains a value beforehand, and the safer unwrap or expect should almost always be preferred. The compiler will typically elide the checks if they program verified the value appropriately before use in order to not panic.

auto unzip() && -> auto
requires
!std::is_reference_v<T>
__private::IsTupleOfSizeTwo<T>::value
auto unzip() const& -> auto
requires
!std::is_reference_v<T>
__private::IsTupleOfSizeTwo<T>::value
sus::mem::CopyOrRef<T>

Unzips an option holding a Tuple of two values into a Tuple of two Options.

Option<Tuple<i32, u32>> is unzipped to Tuple<Option<i32>, Option<u32>>.

If self is Some, the result is a tuple with both options holding the values from self. Otherwise, the result is a tuple of two options set to None.

auto xor_that(Option<T> that) && -> Option<T>
auto xor_that(Option<T> that) const& -> Option<T>
requires
sus::mem::CopyOrRef<T>

Consumes this option and returns an option holding the value from either this option or that option if exactly one of them holds a value, otherwise returns an empty Option.

template <class U, int &... , class Tuple = ::sus::tuple_type::Tuple<T, U>>
auto zip(Option<U> o) && -> Option<Tuple>
template <class U, int &... , class Tuple = ::sus::tuple_type::Tuple<T, U>>
auto zip(Option<U> o) const& -> Option<Tuple>
requires
sus::mem::CopyOrRef<T>

Zips self with another option.

If self is Some(s) and other is Some(o), this method returns Some(Tuple(s, o)). Otherwise, None is returned.

Conversions

An operator which returns the state of the option, either Some or None.

This supports the use of an option in a switch, allowing it to act as a tagged union between "some value" and "no value".

Example

auto x = Option<int>(2);
switch (x) {
 case Some:
  return sus::move(x).unwrap_unchecked(unsafe_fn);
 case None:
  return -1;
}
template <class U>
operator optional() const&
requires
std::same_as<U, T>
!std::is_reference_v<T>
template <class U>
operator optional() &&
requires
std::same_as<U, T>
!std::is_reference_v<T>

Implicit conversion to std::optional.

When the option is holding a reference, it will not convert. Use map to convert to a pointer or cloned to create a cloned value.

Operators

auto operator*() const& -> const std::remove_reference_t<T>&
auto operator*() && -> const std::remove_reference_t<T>&
requires
std::is_reference_v<T>
auto operator*() & -> std::remove_reference_t<T>&

Returns a reference to the contained value inside the option.

A shorthand for as_value and as_value_mut that is less explicit about const. The returned reference is const if the option is accessed as const, and is mutable otherwise. This method allows calling methods directly on the type inside the option without unwrapping.

To extract the value inside an option, use unwrap on an rvalue, and take to move the contents of an lvalue option to an rvalue.

Panics

The function will panic without a message if the option's state is currently None.

Implementation Notes

Implementation note: We only allow calling this on an rvalue option if the contained value is a reference, otherwise we are returning a reference to a short-lived object which leads to common C++ memory bugs.

Implementation note: This method is added in addition to the Rust option API because:

  • C++ moving is verbose, making unwrap on lvalues loud.
  • Unwrapping requires a new lvalue name since C++ doesn't allow name reuse, making variable names bad.
  • We also provide as_value and as_value_mut for explicit const/mutable lvalue access but...
  • It's expected in C++ ecosystems, due to std::optional and other pre-existing collection-of-one things to provide access through operator* and operator->.
auto operator->() const& -> const std::remove_reference_t<T>*
auto operator->() && -> const std::remove_reference_t<T>*
requires
std::is_reference_v<T>
auto operator->() & -> std::remove_reference_t<T>*

Returns a pointer to the contained value inside the option.

The pointer is const if the option is const, and is mutable otherwise. This method allows calling methods directly on the type inside the option without unwrapping.

To extract the value inside an option, use unwrap on an rvalue, and take to move the contents of an lvalue option to an rvalue.

Panics

The function will panic without a message if the option's state is currently None.

Implementation Notes

Implementation note: We only allow calling this on an rvalue option if the contained value is a reference, otherwise we are returning a reference to a short-lived object which leads to common C++ memory bugs.

Implementation note: This method is added in addition to the Rust Option API because:

  • C++ moving is verbose, making unwrap on lvalues loud.
  • Unwrapping requires a new lvalue name since C++ doesn't allow name reuse, making variable names bad.
  • We also provide as_value and as_value_mut for explicit const/mutable lvalue access but...
  • It's expected in C++ ecosystems, due to std::optional and other pre-existing collection-of-one things to provide access through operator* and operator->.
auto operator=(const Option<T>& o) -> Option<T>&
requires
sus::mem::CopyOrRef<T>
auto operator=(const Option<T>& o) -> Option<T>&
requires
!::sus::mem::CopyOrRef<T>
deleted

Copy assignment for Option which will satisfy Copy for Option<T> if T satisifes Copy.

If T can be trivially copy-assigned, then Option<T> can also be trivially copy-assigned.

auto operator=(Option<T>&& o) -> Option<T>&
requires
sus::mem::MoveOrRef<T>
auto operator=(Option<T>&& o) -> Option<T>&
requires
!::sus::mem::MoveOrRef<T>
deleted

Move assignment for Option which will satisfy Move for <Option<T> if T satisifies Move.

If T can be trivially move-assigned, then Option<T> can also be trivially move-assigned. When trivially-moved, the Option is copied on move, and the moved-from Option is unchanged but should still not be used thereafter without reinitializing it. Use take to move the value out of the Option when the Option may be used again afterward, such as with class fields.