From 2b66ac87e0e777fedad3ba226b49b2293188e005 Mon Sep 17 00:00:00 2001 From: benluelo Date: Tue, 19 Jul 2022 03:16:07 -0400 Subject: [PATCH] Add map_continue and continue_value combinators to ControlFlow Fix type error Fix continue_value doc comment --- core/src/ops/control_flow.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/core/src/ops/control_flow.rs b/core/src/ops/control_flow.rs index e34e26746..b1f5559dc 100644 --- a/core/src/ops/control_flow.rs +++ b/core/src/ops/control_flow.rs @@ -195,6 +195,41 @@ impl ControlFlow { 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::::Break(3).continue_value(), None); + /// assert_eq!(ControlFlow::::Continue(3).continue_value(), Some(3)); + /// ``` + #[inline] + #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")] + pub fn continue_value(self) -> Option { + match self { + ControlFlow::Continue(x) => Some(x), + ControlFlow::Break(..) => None, + } + } + + /// Maps `ControlFlow` to `ControlFlow` 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(self, f: F) -> ControlFlow + 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.