@@ -656,23 +656,37 @@ class Option final {
656
656
}
657
657
}
658
658
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.
659
665
template <class E , int &..., class Result = ::sus::result::Result<T, E>>
660
666
constexpr inline Result ok_or (E e) && noexcept {
661
667
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_)));
663
670
else
664
671
return Result::with_err (static_cast <E&&>(e));
665
672
}
666
673
674
+ // / Transforms the `Option<T>` into a `Result<T, E>`, mapping `Some(v)` to
675
+ // / `Ok(v)` and `None` to `Err(f())`.
667
676
template <class ElseFn , int &..., class E = std::invoke_result_t <ElseFn>,
668
677
class Result = ::sus::result::Result<T, E>>
669
678
constexpr inline Result ok_or_else (ElseFn f) && noexcept {
670
679
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_)));
672
682
else
673
683
return Result::with_err (static_cast <ElseFn&&>(f)());
674
684
}
675
685
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(_)`.
676
690
template <int &...,
677
691
class OkType =
678
692
typename ::sus::result::__private::IsResultType<T>::ok_type,
@@ -1118,6 +1132,31 @@ class Option<T&> final {
1118
1132
}
1119
1133
}
1120
1134
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
+
1121
1160
// / Replaces whatever the Option is currently holding with #Some value `t` and
1122
1161
// / returns an Option holding what was there previously.
1123
1162
Option replace (T& t) & noexcept {
0 commit comments