-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
VisitorFlowControl
to some accessible place since it's general…
…ly useful
- Loading branch information
Showing
4 changed files
with
34 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
crates/viewer/re_viewer_context/src/visitor_flow_control.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//! Companion to [`std::ops::ControlFlow`] useful to implement visitor patterns. | ||
use std::ops::ControlFlow; | ||
|
||
/// Type to be returned by visitor closure to control the tree traversal flow. | ||
pub enum VisitorControlFlow<B> { | ||
/// Continue tree traversal | ||
Continue, | ||
|
||
/// Continue tree traversal but skip the children of the current item. | ||
SkipBranch, | ||
|
||
/// Stop traversal and return this value. | ||
Break(B), | ||
} | ||
|
||
impl<B> VisitorControlFlow<B> { | ||
/// Indicates whether we should visit the children of the current node—or entirely stop | ||
/// traversal. | ||
/// | ||
/// Returning a [`ControlFlow`] enables key ergonomics by allowing the use of the short circuit | ||
/// operator (`?`) while extracting the flag to control traversal of children. | ||
pub fn visit_children(self) -> ControlFlow<B, bool> { | ||
match self { | ||
Self::Break(val) => ControlFlow::Break(val), | ||
Self::Continue => ControlFlow::Continue(true), | ||
Self::SkipBranch => ControlFlow::Continue(false), | ||
} | ||
} | ||
} |