3 /// Used to make try_fold closures more like normal loops
4 #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
5 #[derive(Debug, Clone, Copy, PartialEq)]
6 pub enum ControlFlow<B, C = ()> {
7 /// Continue in the loop, using the given value for the next iteration
9 /// Exit the loop, yielding the given value
13 #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
14 impl<B, C> Try for ControlFlow<B, C> {
18 fn into_result(self) -> Result<Self::Ok, Self::Error> {
20 ControlFlow::Continue(y) => Ok(y),
21 ControlFlow::Break(x) => Err(x),
25 fn from_error(v: Self::Error) -> Self {
29 fn from_ok(v: Self::Ok) -> Self {
30 ControlFlow::Continue(v)
34 impl<B, C> ControlFlow<B, C> {
35 /// Returns `true` if this is a `Break` variant.
37 #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
38 pub fn is_break(&self) -> bool {
39 matches!(*self, ControlFlow::Break(_))
42 /// Returns `true` if this is a `Continue` variant.
44 #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
45 pub fn is_continue(&self) -> bool {
46 matches!(*self, ControlFlow::Continue(_))
49 /// Converts the `ControlFlow` into an `Option` which is `Some` if the
50 /// `ControlFlow` was `Break` and `None` otherwise.
52 #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
53 pub fn break_value(self) -> Option<B> {
55 ControlFlow::Continue(..) => None,
56 ControlFlow::Break(x) => Some(x),
60 /// Maps `ControlFlow<B, C>` to `ControlFlow<T, C>` by applying a function
61 /// to the break value in case it exists.
63 #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
64 pub fn map_break<T, F>(self, f: F) -> ControlFlow<T, C>
69 ControlFlow::Continue(x) => ControlFlow::Continue(x),
70 ControlFlow::Break(x) => ControlFlow::Break(f(x)),
75 impl<R: Try> ControlFlow<R, R::Ok> {
76 /// Create a `ControlFlow` from any type implementing `Try`.
77 #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
79 pub fn from_try(r: R) -> Self {
80 match Try::into_result(r) {
81 Ok(v) => ControlFlow::Continue(v),
82 Err(v) => ControlFlow::Break(Try::from_error(v)),
86 /// Convert a `ControlFlow` into any type implementing `Try`;
87 #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
89 pub fn into_try(self) -> R {
91 ControlFlow::Continue(v) => Try::from_ok(v),
92 ControlFlow::Break(v) => v,
97 impl<B> ControlFlow<B, ()> {
98 /// It's frequently the case that there's no value needed with `Continue`,
99 /// so this provides a way to avoid typing `(())`, if you prefer it.
104 /// #![feature(control_flow_enum)]
105 /// use std::ops::ControlFlow;
107 /// let mut partial_sum = 0;
108 /// let last_used = (1..10).chain(20..25).try_for_each(|x| {
109 /// partial_sum += x;
110 /// if partial_sum > 100 { ControlFlow::Break(x) }
111 /// else { ControlFlow::CONTINUE }
113 /// assert_eq!(last_used.break_value(), Some(22));
115 #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
116 pub const CONTINUE: Self = ControlFlow::Continue(());
119 impl<C> ControlFlow<(), C> {
120 /// APIs like `try_for_each` don't need values with `Break`,
121 /// so this provides a way to avoid typing `(())`, if you prefer it.
126 /// #![feature(control_flow_enum)]
127 /// use std::ops::ControlFlow;
129 /// let mut partial_sum = 0;
130 /// (1..10).chain(20..25).try_for_each(|x| {
131 /// if partial_sum > 100 { ControlFlow::BREAK }
132 /// else { partial_sum += x; ControlFlow::CONTINUE }
134 /// assert_eq!(partial_sum, 108);
136 #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
137 pub const BREAK: Self = ControlFlow::Break(());