From f95ec6080725fcd85ad30903666b4b3091797840 Mon Sep 17 00:00:00 2001 From: Antoine Beyeler Date: Wed, 29 Jan 2025 16:20:42 +0100 Subject: [PATCH] Move `VisitorFlowControl` to some accessible place since it's generally useful --- .../re_blueprint_tree/src/blueprint_tree.rs | 3 +- crates/viewer/re_blueprint_tree/src/data.rs | 29 +----------------- crates/viewer/re_viewer_context/src/lib.rs | 2 ++ .../src/visitor_flow_control.rs | 30 +++++++++++++++++++ 4 files changed, 34 insertions(+), 30 deletions(-) create mode 100644 crates/viewer/re_viewer_context/src/visitor_flow_control.rs diff --git a/crates/viewer/re_blueprint_tree/src/blueprint_tree.rs b/crates/viewer/re_blueprint_tree/src/blueprint_tree.rs index 55268507aebf..817c3c11a29c 100644 --- a/crates/viewer/re_blueprint_tree/src/blueprint_tree.rs +++ b/crates/viewer/re_blueprint_tree/src/blueprint_tree.rs @@ -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. diff --git a/crates/viewer/re_blueprint_tree/src/data.rs b/crates/viewer/re_blueprint_tree/src/data.rs index bb836be37b8d..86449bb86d2e 100644 --- a/crates/viewer/re_blueprint_tree/src/data.rs +++ b/crates/viewer/re_blueprint_tree/src/data.rs @@ -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}; @@ -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 { - /// 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 VisitorControlFlow { - /// 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 { - 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> { diff --git a/crates/viewer/re_viewer_context/src/lib.rs b/crates/viewer/re_viewer_context/src/lib.rs index c45f189f6b26..62709a407864 100644 --- a/crates/viewer/re_viewer_context/src/lib.rs +++ b/crates/viewer/re_viewer_context/src/lib.rs @@ -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}, @@ -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 diff --git a/crates/viewer/re_viewer_context/src/visitor_flow_control.rs b/crates/viewer/re_viewer_context/src/visitor_flow_control.rs new file mode 100644 index 000000000000..5d8b5602f109 --- /dev/null +++ b/crates/viewer/re_viewer_context/src/visitor_flow_control.rs @@ -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 { + /// 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 VisitorControlFlow { + /// 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 { + match self { + Self::Break(val) => ControlFlow::Break(val), + Self::Continue => ControlFlow::Continue(true), + Self::SkipBranch => ControlFlow::Continue(false), + } + } +}