Skip to content

Commit 1993a5f

Browse files
committed
Add map_continue and continue_value combinators to ControlFlow
Fix type error Fix continue_value doc comment
1 parent 96c2df8 commit 1993a5f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

library/core/src/ops/control_flow.rs

+35
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,41 @@ impl<B, C> ControlFlow<B, C> {
195195
ControlFlow::Break(x) => ControlFlow::Break(f(x)),
196196
}
197197
}
198+
199+
/// Converts the `ControlFlow` into an `Option` which is `Some` if the
200+
/// `ControlFlow` was `Continue` and `None` otherwise.
201+
///
202+
/// # Examples
203+
///
204+
/// ```
205+
/// #![feature(control_flow_enum)]
206+
/// use std::ops::ControlFlow;
207+
///
208+
/// assert_eq!(ControlFlow::<i32, String>::Break(3).continue_value(), None);
209+
/// assert_eq!(ControlFlow::<String, i32>::Continue(3).continue_value(), Some(3));
210+
/// ```
211+
#[inline]
212+
#[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
213+
pub fn continue_value(self) -> Option<C> {
214+
match self {
215+
ControlFlow::Continue(x) => Some(x),
216+
ControlFlow::Break(..) => None,
217+
}
218+
}
219+
220+
/// Maps `ControlFlow<B, C>` to `ControlFlow<B, T>` by applying a function
221+
/// to the continue value in case it exists.
222+
#[inline]
223+
#[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
224+
pub fn map_continue<T, F>(self, f: F) -> ControlFlow<B, T>
225+
where
226+
F: FnOnce(C) -> T,
227+
{
228+
match self {
229+
ControlFlow::Continue(x) => ControlFlow::Continue(f(x)),
230+
ControlFlow::Break(x) => ControlFlow::Break(x),
231+
}
232+
}
198233
}
199234

200235
/// These are used only as part of implementing the iterator adapters.

0 commit comments

Comments
 (0)