Skip to content

Commit 10b519c

Browse files
committed
Add ok_or and ok_or_else for Option<T&>
1 parent 83b1695 commit 10b519c

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

option/option.h

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,23 +656,37 @@ class Option final {
656656
}
657657
}
658658

659+
/// Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to
660+
/// `Ok(v)` and `None` to `Err(e)`.
661+
///
662+
/// Arguments passed to #ok_or are eagerly evaluated; if you are passing the
663+
/// result of a function call, it is recommended to use ok_or_else, which is
664+
/// lazily evaluated.
659665
template <class E, int&..., class Result = ::sus::result::Result<T, E>>
660666
constexpr inline Result ok_or(E e) && noexcept {
661667
if (t_.set_state(None) == Some)
662-
return Result::with(::sus::mem::take_and_destruct(unsafe_fn, mref(t_.val_)));
668+
return Result::with(
669+
::sus::mem::take_and_destruct(unsafe_fn, mref(t_.val_)));
663670
else
664671
return Result::with_err(static_cast<E&&>(e));
665672
}
666673

674+
/// Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to
675+
/// `Ok(v)` and `None` to `Err(f())`.
667676
template <class ElseFn, int&..., class E = std::invoke_result_t<ElseFn>,
668677
class Result = ::sus::result::Result<T, E>>
669678
constexpr inline Result ok_or_else(ElseFn f) && noexcept {
670679
if (t_.set_state(None) == Some)
671-
return Result::with(::sus::mem::take_and_destruct(unsafe_fn, mref(t_.val_)));
680+
return Result::with(
681+
::sus::mem::take_and_destruct(unsafe_fn, mref(t_.val_)));
672682
else
673683
return Result::with_err(static_cast<ElseFn&&>(f)());
674684
}
675685

686+
/// Transposes an #Option of a #Result into a #Result of an #Option.
687+
///
688+
/// `None` will be mapped to `Ok(None)`. `Some(Ok(_))` and `Some(Err(_))` will
689+
/// be mapped to `Ok(Some(_))` and `Err(_)`.
676690
template <int&...,
677691
class OkType =
678692
typename ::sus::result::__private::IsResultType<T>::ok_type,
@@ -1118,6 +1132,31 @@ class Option<T&> final {
11181132
}
11191133
}
11201134

1135+
/// Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to
1136+
/// `Ok(v)` and `None` to `Err(e)`.
1137+
///
1138+
/// Arguments passed to #ok_or are eagerly evaluated; if you are passing the
1139+
/// result of a function call, it is recommended to use ok_or_else, which is
1140+
/// lazily evaluated.
1141+
template <class E, int&..., class Result = ::sus::result::Result<T&, E>>
1142+
constexpr inline Result ok_or(E e) && noexcept {
1143+
if (t_.state() == Some)
1144+
return Result::with(*::sus::mem::replace_ptr(mref(t_.ptr_), nullptr));
1145+
else
1146+
return Result::with_err(static_cast<E&&>(e));
1147+
}
1148+
1149+
/// Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to
1150+
/// `Ok(v)` and `None` to `Err(f())`.
1151+
template <class ElseFn, int&..., class E = std::invoke_result_t<ElseFn>,
1152+
class Result = ::sus::result::Result<T&, E>>
1153+
constexpr inline Result ok_or_else(ElseFn f) && noexcept {
1154+
if (t_.set_state(None) == Some)
1155+
return Result::with(*::sus::mem::replace_ptr(mref(t_.ptr_), nullptr));
1156+
else
1157+
return Result::with_err(static_cast<ElseFn&&>(f)());
1158+
}
1159+
11211160
/// Replaces whatever the Option is currently holding with #Some value `t` and
11221161
/// returns an Option holding what was there previously.
11231162
Option replace(T& t) & noexcept {

0 commit comments

Comments
 (0)