|
21 | 21 |
|
22 | 22 | // TODO: Clone integration: cloned().
|
23 | 23 |
|
24 |
| -// TODO: Pair/tuple integration: zip(). |
25 |
| - |
26 | 24 | #pragma once
|
27 | 25 |
|
28 | 26 | #include <type_traits>
|
@@ -51,6 +49,11 @@ template <class T, class E>
|
51 | 49 | class Result;
|
52 | 50 | }
|
53 | 51 |
|
| 52 | +namespace sus::tuple { |
| 53 | +template <class T, class... Ts> |
| 54 | +class Tuple; |
| 55 | +} |
| 56 | + |
54 | 57 | namespace sus::option {
|
55 | 58 |
|
56 | 59 | using sus::iter::Iterator;
|
@@ -683,6 +686,25 @@ class Option final {
|
683 | 686 | return Result::with_err(static_cast<ElseFn&&>(f)());
|
684 | 687 | }
|
685 | 688 |
|
| 689 | + /// Zips self with another Option. |
| 690 | + /// |
| 691 | + /// If self is `Some(s)` and other is `Some(o)`, this method returns `Some((s, |
| 692 | + /// o))`. Otherwise, `None` is returned. |
| 693 | + template <class U, int&..., class Tuple = ::sus::tuple::Tuple<T, U>> |
| 694 | + constexpr inline Option<Tuple> zip(Option<U> o) && noexcept { |
| 695 | + if (o.is_none()) { |
| 696 | + if (t_.set_state(None) == Some) t_.val_.~T(); |
| 697 | + return Option<Tuple>::none(); |
| 698 | + } else if (is_none()) { |
| 699 | + return Option<Tuple>::none(); |
| 700 | + } else { |
| 701 | + t_.set_state(None); |
| 702 | + return Option<Tuple>::some( |
| 703 | + Tuple::with(::sus::mem::take_and_destruct(unsafe_fn, mref(t_.val_)), |
| 704 | + static_cast<Option<U>&&>(o).unwrap())); |
| 705 | + } |
| 706 | + } |
| 707 | + |
686 | 708 | /// Transposes an #Option of a #Result into a #Result of an #Option.
|
687 | 709 | ///
|
688 | 710 | /// `None` will be mapped to `Ok(None)`. `Some(Ok(_))` and `Some(Err(_))` will
|
@@ -1157,6 +1179,24 @@ class Option<T&> final {
|
1157 | 1179 | return Result::with_err(static_cast<ElseFn&&>(f)());
|
1158 | 1180 | }
|
1159 | 1181 |
|
| 1182 | + /// Zips self with another Option. |
| 1183 | + /// |
| 1184 | + /// If self is `Some(s)` and other is `Some(o)`, this method returns `Some((s, |
| 1185 | + /// o))`. Otherwise, `None` is returned. |
| 1186 | + template <class U, int&..., class Tuple = ::sus::tuple::Tuple<T&, U>> |
| 1187 | + constexpr inline Option<Tuple> zip(Option<U> o) && noexcept { |
| 1188 | + if (o.is_none()) { |
| 1189 | + t_.ptr_ = nullptr; |
| 1190 | + return Option<Tuple>::none(); |
| 1191 | + } else if (is_none()) { |
| 1192 | + return Option<Tuple>::none(); |
| 1193 | + } else { |
| 1194 | + return Option<Tuple>::some( |
| 1195 | + Tuple::with(*::sus::mem::replace_ptr(mref(t_.ptr_), nullptr), |
| 1196 | + static_cast<Option<U>&&>(o).unwrap())); |
| 1197 | + } |
| 1198 | + } |
| 1199 | + |
1160 | 1200 | /// Replaces whatever the Option is currently holding with #Some value `t` and
|
1161 | 1201 | /// returns an Option holding what was there previously.
|
1162 | 1202 | Option replace(T& t) & noexcept {
|
|
0 commit comments