]> git.lizzy.rs Git - rust.git/blob - library/core/src/ops/control_flow.rs
Use `summary_opts()` in another spot
[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
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")]
64     #[inline]
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)),
69         }
70     }
71
72     /// Convert a `ControlFlow` into any type implementing `Try`;
73     #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
74     #[inline]
75     pub fn into_try(self) -> R {
76         match self {
77             ControlFlow::Continue(v) => Try::from_ok(v),
78             ControlFlow::Break(v) => v,
79         }
80     }
81 }
82
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.
86     ///
87     /// # Examples
88     ///
89     /// ```
90     /// #![feature(control_flow_enum)]
91     /// use std::ops::ControlFlow;
92     ///
93     /// let mut partial_sum = 0;
94     /// let last_used = (1..10).chain(20..25).try_for_each(|x| {
95     ///     partial_sum += x;
96     ///     if partial_sum > 100 { ControlFlow::Break(x) }
97     ///     else { ControlFlow::CONTINUE }
98     /// });
99     /// assert_eq!(last_used.break_value(), Some(22));
100     /// ```
101     #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
102     pub const CONTINUE: Self = ControlFlow::Continue(());
103 }
104
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.
108     ///
109     /// # Examples
110     ///
111     /// ```
112     /// #![feature(control_flow_enum)]
113     /// use std::ops::ControlFlow;
114     ///
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 }
119     /// });
120     /// assert_eq!(partial_sum, 108);
121     /// ```
122     #[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
123     pub const BREAK: Self = ControlFlow::Break(());
124 }