Skip to content

Commit af7cd54

Browse files
authored
Rollup merge of rust-lang#76318 - scottmcm:one-control-flow, r=ecstatic-morse
Use ops::ControlFlow in rustc_data_structures::graph::iterate Since I only know about this because you mentioned it, r? @ecstatic-morse If we're not supposed to use new `core` things in compiler for a while then feel free to close, but it felt reasonable to merge the two types since they're the same, and it might be convenient for people to use `?` in their traversal code. (This doesn't do the type parameter swap; NoraCodes has signed up to do that one.)
2 parents c1ac46a + 35c8f49 commit af7cd54

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

core/src/iter/adapters/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ where
12731273
) -> impl FnMut((), T) -> ControlFlow<(), B> + '_ {
12741274
move |(), x| match f(x) {
12751275
Some(x) => ControlFlow::Break(x),
1276-
None => ControlFlow::Continue(()),
1276+
None => ControlFlow::CONTINUE,
12771277
}
12781278
}
12791279

core/src/iter/traits/double_ended.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ pub trait DoubleEndedIterator: Iterator {
310310
mut predicate: impl FnMut(&T) -> bool,
311311
) -> impl FnMut((), T) -> ControlFlow<(), T> {
312312
move |(), x| {
313-
if predicate(&x) { ControlFlow::Break(x) } else { ControlFlow::Continue(()) }
313+
if predicate(&x) { ControlFlow::Break(x) } else { ControlFlow::CONTINUE }
314314
}
315315
}
316316

core/src/iter/traits/iterator.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,10 +2076,10 @@ pub trait Iterator {
20762076
#[inline]
20772077
fn check<T>(mut f: impl FnMut(T) -> bool) -> impl FnMut((), T) -> ControlFlow<(), ()> {
20782078
move |(), x| {
2079-
if f(x) { ControlFlow::Continue(()) } else { ControlFlow::Break(()) }
2079+
if f(x) { ControlFlow::CONTINUE } else { ControlFlow::BREAK }
20802080
}
20812081
}
2082-
self.try_fold((), check(f)) == ControlFlow::Continue(())
2082+
self.try_fold((), check(f)) == ControlFlow::CONTINUE
20832083
}
20842084

20852085
/// Tests if any element of the iterator matches a predicate.
@@ -2129,11 +2129,11 @@ pub trait Iterator {
21292129
#[inline]
21302130
fn check<T>(mut f: impl FnMut(T) -> bool) -> impl FnMut((), T) -> ControlFlow<(), ()> {
21312131
move |(), x| {
2132-
if f(x) { ControlFlow::Break(()) } else { ControlFlow::Continue(()) }
2132+
if f(x) { ControlFlow::BREAK } else { ControlFlow::CONTINUE }
21332133
}
21342134
}
21352135

2136-
self.try_fold((), check(f)) == ControlFlow::Break(())
2136+
self.try_fold((), check(f)) == ControlFlow::BREAK
21372137
}
21382138

21392139
/// Searches for an element of an iterator that satisfies a predicate.
@@ -2191,7 +2191,7 @@ pub trait Iterator {
21912191
mut predicate: impl FnMut(&T) -> bool,
21922192
) -> impl FnMut((), T) -> ControlFlow<(), T> {
21932193
move |(), x| {
2194-
if predicate(&x) { ControlFlow::Break(x) } else { ControlFlow::Continue(()) }
2194+
if predicate(&x) { ControlFlow::Break(x) } else { ControlFlow::CONTINUE }
21952195
}
21962196
}
21972197

@@ -2226,7 +2226,7 @@ pub trait Iterator {
22262226
) -> impl FnMut((), T) -> ControlFlow<(), B> {
22272227
move |(), x| match f(x) {
22282228
Some(x) => ControlFlow::Break(x),
2229-
None => ControlFlow::Continue(()),
2229+
None => ControlFlow::CONTINUE,
22302230
}
22312231
}
22322232

@@ -2268,7 +2268,7 @@ pub trait Iterator {
22682268
R: Try<Ok = bool>,
22692269
{
22702270
move |(), x| match f(&x).into_result() {
2271-
Ok(false) => ControlFlow::Continue(()),
2271+
Ok(false) => ControlFlow::CONTINUE,
22722272
Ok(true) => ControlFlow::Break(Ok(x)),
22732273
Err(x) => ControlFlow::Break(Err(x)),
22742274
}

core/src/ops/control_flow.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,46 @@ impl<R: Try> ControlFlow<R::Ok, R> {
6565
}
6666
}
6767
}
68+
69+
impl<B> ControlFlow<(), B> {
70+
/// It's frequently the case that there's no value needed with `Continue`,
71+
/// 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+
/// ```
87+
#[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
88+
pub const CONTINUE: Self = ControlFlow::Continue(());
89+
}
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)