Use ops::ControlFlow in graph::iterate

This commit is contained in:
Scott McMurray 2020-09-04 00:59:41 -07:00
parent 0d0f6b1130
commit fac272688e
7 changed files with 19 additions and 13 deletions

View File

@ -87,11 +87,8 @@ where
} }
/// Allows searches to terminate early with a value. /// Allows searches to terminate early with a value.
#[derive(Clone, Copy, Debug)] // FIXME (#75744): remove the alias once the generics are in a better order and `C=()`.
pub enum ControlFlow<T> { pub type ControlFlow<T> = std::ops::ControlFlow<(), T>;
Break(T),
Continue,
}
/// The status of a node in the depth-first search. /// The status of a node in the depth-first search.
/// ///
@ -260,12 +257,12 @@ where
_node: G::Node, _node: G::Node,
_prior_status: Option<NodeStatus>, _prior_status: Option<NodeStatus>,
) -> ControlFlow<Self::BreakVal> { ) -> ControlFlow<Self::BreakVal> {
ControlFlow::Continue ControlFlow::CONTINUE
} }
/// Called after all nodes reachable from this one have been examined. /// Called after all nodes reachable from this one have been examined.
fn node_settled(&mut self, _node: G::Node) -> ControlFlow<Self::BreakVal> { fn node_settled(&mut self, _node: G::Node) -> ControlFlow<Self::BreakVal> {
ControlFlow::Continue ControlFlow::CONTINUE
} }
/// Behave as if no edges exist from `source` to `target`. /// Behave as if no edges exist from `source` to `target`.
@ -290,7 +287,7 @@ where
) -> ControlFlow<Self::BreakVal> { ) -> ControlFlow<Self::BreakVal> {
match prior_status { match prior_status {
Some(NodeStatus::Visited) => ControlFlow::Break(()), Some(NodeStatus::Visited) => ControlFlow::Break(()),
_ => ControlFlow::Continue, _ => ControlFlow::CONTINUE,
} }
} }
} }

View File

@ -8,6 +8,7 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")] #![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
#![allow(incomplete_features)] #![allow(incomplete_features)]
#![feature(control_flow_enum)]
#![feature(in_band_lifetimes)] #![feature(in_band_lifetimes)]
#![feature(unboxed_closures)] #![feature(unboxed_closures)]
#![feature(generators)] #![feature(generators)]

View File

@ -6,6 +6,7 @@
#![feature(box_syntax)] #![feature(box_syntax)]
#![feature(const_fn)] #![feature(const_fn)]
#![feature(const_panic)] #![feature(const_panic)]
#![feature(control_flow_enum)]
#![feature(crate_visibility_modifier)] #![feature(crate_visibility_modifier)]
#![feature(bool_to_option)] #![feature(bool_to_option)]
#![feature(or_patterns)] #![feature(or_patterns)]

View File

@ -117,7 +117,7 @@ impl<'mir, 'tcx> TriColorVisitor<&'mir Body<'tcx>> for Search<'mir, 'tcx> {
// A diverging InlineAsm is treated as non-recursing // A diverging InlineAsm is treated as non-recursing
TerminatorKind::InlineAsm { destination, .. } => { TerminatorKind::InlineAsm { destination, .. } => {
if destination.is_some() { if destination.is_some() {
ControlFlow::Continue ControlFlow::CONTINUE
} else { } else {
ControlFlow::Break(NonRecursive) ControlFlow::Break(NonRecursive)
} }
@ -131,7 +131,7 @@ impl<'mir, 'tcx> TriColorVisitor<&'mir Body<'tcx>> for Search<'mir, 'tcx> {
| TerminatorKind::FalseEdge { .. } | TerminatorKind::FalseEdge { .. }
| TerminatorKind::FalseUnwind { .. } | TerminatorKind::FalseUnwind { .. }
| TerminatorKind::Goto { .. } | TerminatorKind::Goto { .. }
| TerminatorKind::SwitchInt { .. } => ControlFlow::Continue, | TerminatorKind::SwitchInt { .. } => ControlFlow::CONTINUE,
} }
} }
@ -144,7 +144,7 @@ impl<'mir, 'tcx> TriColorVisitor<&'mir Body<'tcx>> for Search<'mir, 'tcx> {
} }
} }
ControlFlow::Continue ControlFlow::CONTINUE
} }
fn ignore_edge(&mut self, bb: BasicBlock, target: BasicBlock) -> bool { fn ignore_edge(&mut self, bb: BasicBlock, target: BasicBlock) -> bool {

View File

@ -1273,7 +1273,7 @@ where
) -> impl FnMut((), T) -> ControlFlow<(), B> + '_ { ) -> impl FnMut((), T) -> ControlFlow<(), B> + '_ {
move |(), x| match f(x) { move |(), x| match f(x) {
Some(x) => ControlFlow::Break(x), Some(x) => ControlFlow::Break(x),
None => ControlFlow::Continue(()), None => ControlFlow::CONTINUE,
} }
} }

View File

@ -310,7 +310,7 @@ pub trait DoubleEndedIterator: Iterator {
mut predicate: impl FnMut(&T) -> bool, mut predicate: impl FnMut(&T) -> bool,
) -> impl FnMut((), T) -> ControlFlow<(), T> { ) -> impl FnMut((), T) -> ControlFlow<(), T> {
move |(), x| { move |(), x| {
if predicate(&x) { ControlFlow::Break(x) } else { ControlFlow::Continue(()) } if predicate(&x) { ControlFlow::Break(x) } else { ControlFlow::CONTINUE }
} }
} }

View File

@ -65,3 +65,10 @@ impl<R: Try> ControlFlow<R::Ok, R> {
} }
} }
} }
impl<B> ControlFlow<(), B> {
/// It's frequently the case that there's no value needed with `Continue`,
/// so this provides a way to avoid typing `(())`, if you prefer it.
#[unstable(feature = "control_flow_enum", reason = "new API", issue = "75744")]
pub const CONTINUE: Self = ControlFlow::Continue(());
}