Skip to content

Commit e836a2f

Browse files
committed
implement continue_ok and break_ok for ControlFlow
1 parent 213d946 commit e836a2f

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

library/core/src/ops/control_flow.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,28 @@ impl<B, C> ControlFlow<B, C> {
187187
}
188188
}
189189

190+
/// Converts the `ControlFlow` into an `Result` which is `Ok` if the
191+
/// `ControlFlow` was `Break` and `Err` if otherwise.
192+
///
193+
/// # Examples
194+
///
195+
/// ```
196+
/// #![feature(control_flow_ok)]
197+
///
198+
/// use std::ops::ControlFlow;
199+
///
200+
/// assert_eq!(ControlFlow::<&str, i32>::Break("Stop right there!").break_ok(), Ok("Stop right there!"));
201+
/// assert_eq!(ControlFlow::<&str, i32>::Continue(3).break_ok(), Err(3));
202+
/// ```
203+
#[inline]
204+
#[unstable(feature = "control_flow_ok", issue = "140266")]
205+
pub fn break_ok(self) -> Result<B, C> {
206+
match self {
207+
ControlFlow::Continue(c) => Err(c),
208+
ControlFlow::Break(b) => Ok(b),
209+
}
210+
}
211+
190212
/// Maps `ControlFlow<B, C>` to `ControlFlow<T, C>` by applying a function
191213
/// to the break value in case it exists.
192214
#[inline]
@@ -218,6 +240,28 @@ impl<B, C> ControlFlow<B, C> {
218240
}
219241
}
220242

243+
/// Converts the `ControlFlow` into an `Result` which is `Ok` if the
244+
/// `ControlFlow` was `Continue` and `Err` if otherwise.
245+
///
246+
/// # Examples
247+
///
248+
/// ```
249+
/// #![feature(control_flow_ok)]
250+
///
251+
/// use std::ops::ControlFlow;
252+
///
253+
/// assert_eq!(ControlFlow::<&str, i32>::Break("Stop right there!").continue_ok(), Err("Stop right there!"));
254+
/// assert_eq!(ControlFlow::<&str, i32>::Continue(3).continue_ok(), Ok(3));
255+
/// ```
256+
#[inline]
257+
#[unstable(feature = "control_flow_ok", issue = "140266")]
258+
pub fn continue_ok(self) -> Result<C, B> {
259+
match self {
260+
ControlFlow::Continue(c) => Ok(c),
261+
ControlFlow::Break(b) => Err(b),
262+
}
263+
}
264+
221265
/// Maps `ControlFlow<B, C>` to `ControlFlow<B, T>` by applying a function
222266
/// to the continue value in case it exists.
223267
#[inline]

0 commit comments

Comments
 (0)