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),
61 impl<R: Try> ControlFlow<R, R::Ok> {
62 /// Create a `ControlFlow` from any type implementing `Try`.
63 #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
65 pub fn from_try(r: R) -> Self {
66 match Try::into_result(r) {
67 Ok(v) => ControlFlow::Continue(v),
68 Err(v) => ControlFlow::Break(Try::from_error(v)),
72 /// Convert a `ControlFlow` into any type implementing `Try`;
73 #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
75 pub fn into_try(self) -> R {
77 ControlFlow::Continue(v) => Try::from_ok(v),
78 ControlFlow::Break(v) => v,
83 impl<B> ControlFlow<B, ()> {
84 /// It's frequently the case that there's no value needed with `Continue`,
85 /// so this provides a way to avoid typing `(())`, if you prefer it.
90 /// #![feature(control_flow_enum)]
91 /// use std::ops::ControlFlow;
93 /// let mut partial_sum = 0;
94 /// let last_used = (1..10).chain(20..25).try_for_each(|x| {
96 /// if partial_sum > 100 { ControlFlow::Break(x) }
97 /// else { ControlFlow::CONTINUE }
99 /// assert_eq!(last_used.break_value(), Some(22));
101 #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
102 pub const CONTINUE: Self = ControlFlow::Continue(());
105 impl<C> ControlFlow<(), C> {
106 /// APIs like `try_for_each` don't need values with `Break`,
107 /// so this provides a way to avoid typing `(())`, if you prefer it.
112 /// #![feature(control_flow_enum)]
113 /// use std::ops::ControlFlow;
115 /// let mut partial_sum = 0;
116 /// (1..10).chain(20..25).try_for_each(|x| {
117 /// if partial_sum > 100 { ControlFlow::BREAK }
118 /// else { partial_sum += x; ControlFlow::CONTINUE }
120 /// assert_eq!(partial_sum, 108);
122 #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
123 pub const BREAK: Self = ControlFlow::Break(());