]> git.lizzy.rs Git - rust.git/blob - library/core/src/ops/control_flow.rs
Auto merge of #79780 - camelid:use-summary_opts, r=GuillaumeGomez
[rust.git] / library / core / src / ops / control_flow.rs
1 use crate::ops::Try;
2
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
8     Continue(C),
9     /// Exit the loop, yielding the given value
10     Break(B),
11 }
12
13 #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
14 impl<B, C> Try for ControlFlow<B, C> {
15     type Ok = C;
16     type Error = B;
17     #[inline]
18     fn into_result(self) -> Result<Self::Ok, Self::Error> {
19         match self {
20             ControlFlow::Continue(y) => Ok(y),
21             ControlFlow::Break(x) => Err(x),
22         }
23     }
24     #[inline]
25     fn from_error(v: Self::Error) -> Self {
26         ControlFlow::Break(v)
27     }
28     #[inline]
29     fn from_ok(v: Self::Ok) -> Self {
30         ControlFlow::Continue(v)
31     }
32 }
33
34 impl<B, C> ControlFlow<B, C> {
35     /// Returns `true` if this is a `Break` variant.
36     #[inline]
37     #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
38     pub fn is_break(&self) -> bool {
39         matches!(*self, ControlFlow::Break(_))
40     }
41
42     /// Returns `true` if this is a `Continue` variant.
43     #[inline]
44     #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
45     pub fn is_continue(&self) -> bool {
46         matches!(*self, ControlFlow::Continue(_))
47     }
48
49     /// Converts the `ControlFlow` into an `Option` which is `Some` if the
50     /// `ControlFlow` was `Break` and `None` otherwise.
51     #[inline]
52     #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
53     pub fn break_value(self) -> Option<B> {
54         match self {
55             ControlFlow::Continue(..) => None,
56             ControlFlow::Break(x) => Some(x),
57         }
58     }
59
60     /// Maps `ControlFlow<B, C>` to `ControlFlow<T, C>` by applying a function
61     /// to the break value in case it exists.
62     #[inline]
63     #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
64     pub fn map_break<T, F>(self, f: F) -> ControlFlow<T, C>
65     where
66         F: FnOnce(B) -> T,
67     {
68         match self {
69             ControlFlow::Continue(x) => ControlFlow::Continue(x),
70             ControlFlow::Break(x) => ControlFlow::Break(f(x)),
71         }
72     }
73 }
74
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")]
78     #[inline]
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)),
83         }
84     }
85
86     /// Convert a `ControlFlow` into any type implementing `Try`;
87     #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
88     #[inline]
89     pub fn into_try(self) -> R {
90         match self {
91             ControlFlow::Continue(v) => Try::from_ok(v),
92             ControlFlow::Break(v) => v,
93         }
94     }
95 }
96
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.
100     ///
101     /// # Examples
102     ///
103     /// ```
104     /// #![feature(control_flow_enum)]
105     /// use std::ops::ControlFlow;
106     ///
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 }
112     /// });
113     /// assert_eq!(last_used.break_value(), Some(22));
114     /// ```
115     #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
116     pub const CONTINUE: Self = ControlFlow::Continue(());
117 }
118
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.
122     ///
123     /// # Examples
124     ///
125     /// ```
126     /// #![feature(control_flow_enum)]
127     /// use std::ops::ControlFlow;
128     ///
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 }
133     /// });
134     /// assert_eq!(partial_sum, 108);
135     /// ```
136     #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
137     pub const BREAK: Self = ControlFlow::Break(());
138 }