diff options
| author | benluelo <benluelo@hotmail.com> | 2022-07-19 03:16:07 -0400 |
|---|---|---|
| committer | benluelo <benluelo@hotmail.com> | 2022-07-20 16:32:09 -0400 |
| commit | 1993a5f7a866f174aa50329a03b2f8b2f589221c (patch) | |
| tree | 4e8e930bc71aa18f64c5ba8b3ffe88f8a181ae4d | |
| parent | 96c2df810b0b681fee63cae11ca63844792b6190 (diff) | |
| download | rust-1993a5f7a866f174aa50329a03b2f8b2f589221c.tar.gz rust-1993a5f7a866f174aa50329a03b2f8b2f589221c.zip | |
Add map_continue and continue_value combinators to ControlFlow
Fix type error Fix continue_value doc comment
| -rw-r--r-- | library/core/src/ops/control_flow.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/library/core/src/ops/control_flow.rs b/library/core/src/ops/control_flow.rs index e34e26746c0..b1f5559dcfc 100644 --- a/library/core/src/ops/control_flow.rs +++ b/library/core/src/ops/control_flow.rs @@ -195,6 +195,41 @@ impl<B, C> ControlFlow<B, C> { ControlFlow::Break(x) => ControlFlow::Break(f(x)), } } + + /// Converts the `ControlFlow` into an `Option` which is `Some` if the + /// `ControlFlow` was `Continue` and `None` otherwise. + /// + /// # Examples + /// + /// ``` + /// #![feature(control_flow_enum)] + /// use std::ops::ControlFlow; + /// + /// assert_eq!(ControlFlow::<i32, String>::Break(3).continue_value(), None); + /// assert_eq!(ControlFlow::<String, i32>::Continue(3).continue_value(), Some(3)); + /// ``` + #[inline] + #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")] + pub fn continue_value(self) -> Option<C> { + match self { + ControlFlow::Continue(x) => Some(x), + ControlFlow::Break(..) => None, + } + } + + /// Maps `ControlFlow<B, C>` to `ControlFlow<B, T>` by applying a function + /// to the continue value in case it exists. + #[inline] + #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")] + pub fn map_continue<T, F>(self, f: F) -> ControlFlow<B, T> + where + F: FnOnce(C) -> T, + { + match self { + ControlFlow::Continue(x) => ControlFlow::Continue(f(x)), + ControlFlow::Break(x) => ControlFlow::Break(x), + } + } } /// These are used only as part of implementing the iterator adapters. |
