Skip to content

Commit 59e3733

Browse files
committed
Add BREAK too, and improve the comments
1 parent fac2726 commit 59e3733

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

compiler/rustc_data_structures/src/graph/iterate/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ where
286286
prior_status: Option<NodeStatus>,
287287
) -> ControlFlow<Self::BreakVal> {
288288
match prior_status {
289-
Some(NodeStatus::Visited) => ControlFlow::Break(()),
289+
Some(NodeStatus::Visited) => ControlFlow::BREAK,
290290
_ => ControlFlow::CONTINUE,
291291
}
292292
}

library/core/src/iter/traits/iterator.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -2086,10 +2086,10 @@ pub trait Iterator {
20862086
#[inline]
20872087
fn check<T>(mut f: impl FnMut(T) -> bool) -> impl FnMut((), T) -> ControlFlow<(), ()> {
20882088
move |(), x| {
2089-
if f(x) { ControlFlow::Continue(()) } else { ControlFlow::Break(()) }
2089+
if f(x) { ControlFlow::CONTINUE } else { ControlFlow::BREAK }
20902090
}
20912091
}
2092-
self.try_fold((), check(f)) == ControlFlow::Continue(())
2092+
self.try_fold((), check(f)) == ControlFlow::CONTINUE
20932093
}
20942094

20952095
/// Tests if any element of the iterator matches a predicate.
@@ -2139,11 +2139,11 @@ pub trait Iterator {
21392139
#[inline]
21402140
fn check<T>(mut f: impl FnMut(T) -> bool) -> impl FnMut((), T) -> ControlFlow<(), ()> {
21412141
move |(), x| {
2142-
if f(x) { ControlFlow::Break(()) } else { ControlFlow::Continue(()) }
2142+
if f(x) { ControlFlow::BREAK } else { ControlFlow::CONTINUE }
21432143
}
21442144
}
21452145

2146-
self.try_fold((), check(f)) == ControlFlow::Break(())
2146+
self.try_fold((), check(f)) == ControlFlow::BREAK
21472147
}
21482148

21492149
/// Searches for an element of an iterator that satisfies a predicate.
@@ -2201,7 +2201,7 @@ pub trait Iterator {
22012201
mut predicate: impl FnMut(&T) -> bool,
22022202
) -> impl FnMut((), T) -> ControlFlow<(), T> {
22032203
move |(), x| {
2204-
if predicate(&x) { ControlFlow::Break(x) } else { ControlFlow::Continue(()) }
2204+
if predicate(&x) { ControlFlow::Break(x) } else { ControlFlow::CONTINUE }
22052205
}
22062206
}
22072207

@@ -2236,7 +2236,7 @@ pub trait Iterator {
22362236
) -> impl FnMut((), T) -> ControlFlow<(), B> {
22372237
move |(), x| match f(x) {
22382238
Some(x) => ControlFlow::Break(x),
2239-
None => ControlFlow::Continue(()),
2239+
None => ControlFlow::CONTINUE,
22402240
}
22412241
}
22422242

@@ -2278,7 +2278,7 @@ pub trait Iterator {
22782278
R: Try<Ok = bool>,
22792279
{
22802280
move |(), x| match f(&x).into_result() {
2281-
Ok(false) => ControlFlow::Continue(()),
2281+
Ok(false) => ControlFlow::CONTINUE,
22822282
Ok(true) => ControlFlow::Break(Ok(x)),
22832283
Err(x) => ControlFlow::Break(Err(x)),
22842284
}

library/core/src/ops/control_flow.rs

+36
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,42 @@ impl<R: Try> ControlFlow<R::Ok, R> {
6969
impl<B> ControlFlow<(), B> {
7070
/// It's frequently the case that there's no value needed with `Continue`,
7171
/// so this provides a way to avoid typing `(())`, if you prefer it.
72+
///
73+
/// # Examples
74+
///
75+
/// ```
76+
/// #![feature(control_flow_enum)]
77+
/// use std::ops::ControlFlow;
78+
///
79+
/// let mut partial_sum = 0;
80+
/// let last_used = (1..10).chain(20..25).try_for_each(|x| {
81+
/// partial_sum += x;
82+
/// if partial_sum > 100 { ControlFlow::Break(x) }
83+
/// else { ControlFlow::CONTINUE }
84+
/// });
85+
/// assert_eq!(last_used.break_value(), Some(22));
86+
/// ```
7287
#[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
7388
pub const CONTINUE: Self = ControlFlow::Continue(());
7489
}
90+
91+
impl<C> ControlFlow<C, ()> {
92+
/// APIs like `try_for_each` don't need values with `Break`,
93+
/// so this provides a way to avoid typing `(())`, if you prefer it.
94+
///
95+
/// # Examples
96+
///
97+
/// ```
98+
/// #![feature(control_flow_enum)]
99+
/// use std::ops::ControlFlow;
100+
///
101+
/// let mut partial_sum = 0;
102+
/// (1..10).chain(20..25).try_for_each(|x| {
103+
/// if partial_sum > 100 { ControlFlow::BREAK }
104+
/// else { partial_sum += x; ControlFlow::CONTINUE }
105+
/// });
106+
/// assert_eq!(partial_sum, 108);
107+
/// ```
108+
#[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
109+
pub const BREAK: Self = ControlFlow::Break(());
110+
}

0 commit comments

Comments
 (0)