Skip to content

Commit

Permalink
Move VisitorFlowControl to some accessible place since it's general…
Browse files Browse the repository at this point in the history
…ly useful
  • Loading branch information
abey79 committed Jan 29, 2025
1 parent a0f49ae commit f95ec60
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
3 changes: 1 addition & 2 deletions crates/viewer/re_blueprint_tree/src/blueprint_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ use re_ui::{
use re_viewer_context::{
contents_name_style, icon_for_container_kind, CollapseScope, ContainerId, Contents,
DragAndDropFeedback, DragAndDropPayload, HoverHighlight, Item, ItemContext,
SystemCommandSender, ViewId, ViewerContext,
SystemCommandSender, ViewId, ViewerContext, VisitorControlFlow,
};
use re_viewport_blueprint::{ui::show_add_view_or_container_modal, ViewportBlueprint};

use crate::data::{
BlueprintTreeData, ContainerData, ContentsData, DataResultData, DataResultKind, ViewData,
VisitorControlFlow,
};

/// Holds the state of the blueprint tree UI.
Expand Down
29 changes: 1 addition & 28 deletions crates/viewer/re_blueprint_tree/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use re_types::blueprint::components::Visible;
use re_ui::filter_widget::FilterMatcher;
use re_viewer_context::{
CollapseScope, ContainerId, Contents, ContentsName, DataQueryResult, DataResultNode, Item,
ViewId, ViewerContext,
ViewId, ViewerContext, VisitorControlFlow,
};
use re_viewport_blueprint::{ContainerBlueprint, ViewBlueprint, ViewportBlueprint};

Expand Down Expand Up @@ -589,33 +589,6 @@ fn default_open_for_data_result(num_children: usize) -> bool {

// ---

/// Returned by the visitor closure to control tree traversal.
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.
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),
}
}
}

/// Wrapper structure used for the closure of [`BlueprintTreeData::visit`] and friends.
#[derive(Debug, Clone, Copy)]
pub enum BlueprintTreeItem<'a> {
Expand Down
2 changes: 2 additions & 0 deletions crates/viewer/re_viewer_context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub mod test_context;

// TODO(andreas): Move to its own crate?
pub mod gpu_bridge;
mod visitor_flow_control;

pub use self::{
annotations::{AnnotationMap, Annotations, ResolvedAnnotationInfo, ResolvedAnnotationInfos},
Expand Down Expand Up @@ -87,6 +88,7 @@ pub use self::{
VisualizableFilterContext, VisualizerCollection, VisualizerQueryInfo, VisualizerSystem,
},
viewer_context::{RecordingConfig, ViewerContext},
visitor_flow_control::VisitorControlFlow,
};

pub use re_ui::UiLayout; // Historical reasons
Expand Down
30 changes: 30 additions & 0 deletions crates/viewer/re_viewer_context/src/visitor_flow_control.rs
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),
}
}
}

0 comments on commit f95ec60

Please sign in to comment.