diff --git a/src/dock_state/mod.rs b/src/dock_state/mod.rs index c385df5..4e7d3c4 100644 --- a/src/dock_state/mod.rs +++ b/src/dock_state/mod.rs @@ -43,11 +43,7 @@ impl std::ops::Index for DockState { #[inline(always)] fn index(&self, index: SurfaceIndex) -> &Self::Output { - let Some(tree) = self - .surfaces - .get(&index) - .and_then(|surface| surface.node_tree()) - else { + let Some(tree) = self.surfaces.get(&index).map(|surface| surface.node_tree()) else { panic!("There did not exist a tree at surface index {}", index.0); }; tree @@ -60,7 +56,7 @@ impl std::ops::IndexMut for DockState { let Some(tree) = self .surfaces .get_mut(&index) - .and_then(|surface| surface.node_tree_mut()) + .map(|surface| surface.node_tree_mut()) else { panic!("There did not exist a tree at surface index {}", index.0); }; @@ -162,9 +158,7 @@ impl DockState { /// Returns true if the specified surface exists and isn't [`Empty`](Surface::Empty). #[inline] pub fn is_surface_valid(&self, surface_index: SurfaceIndex) -> bool { - self.surfaces - .get(&surface_index) - .map_or(false, |surface| !surface.is_empty()) + self.surfaces.get(&surface_index).is_some() } /// Returns a list of all valid [`SurfaceIndex`]es. @@ -447,8 +441,7 @@ impl DockState { pub fn iter_nodes(&self) -> impl Iterator> { self.surfaces .values() - .filter_map(|surface| surface.node_tree()) - .flat_map(|nodes| nodes.iter()) + .flat_map(|surface| surface.node_tree().iter()) } /// Returns a new [`DockState`] while mapping and filtering the tab type. @@ -476,7 +469,7 @@ impl DockState { .iter() .filter_map(|(idx, surface)| { let surface = surface.filter_map_tabs(function.clone()); - (!surface.is_empty()).then_some((*idx, surface)) + (!surface.node_tree().is_empty()).then_some((*idx, surface)) }) .collect(); DockState { @@ -542,7 +535,7 @@ impl DockState { .values_mut() .for_each(|surface| surface.retain_tabs(predicate.clone())); self.surfaces - .retain(|idx, surface| idx.is_main() || !surface.is_empty()); + .retain(|idx, surface| idx.is_main() || !surface.node_tree().is_empty()); } } @@ -560,11 +553,9 @@ where /// /// See also: [`find_main_surface_tab`](DockState::find_main_surface_tab) pub fn find_tab(&self, needle_tab: &Tab) -> Option<(SurfaceIndex, NodeIndex, TabIndex)> { - for &surface_index in self.valid_surface_indices().iter() { - if !self.surfaces[&surface_index].is_empty() { - if let Some((node_index, tab_index)) = self[surface_index].find_tab(needle_tab) { - return Some((surface_index, node_index, tab_index)); - } + for (surface_index, surface) in self.surfaces.iter() { + if let Some((node_index, tab_index)) = surface.node_tree().find_tab(needle_tab) { + return Some((*surface_index, node_index, tab_index)); } } None diff --git a/src/dock_state/surface.rs b/src/dock_state/surface.rs index e0c7668..d7f48a4 100644 --- a/src/dock_state/surface.rs +++ b/src/dock_state/surface.rs @@ -6,9 +6,6 @@ use crate::{Node, NodeIndex, Tree, WindowState}; #[derive(Clone, Debug)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub enum Surface { - /// An empty surface, with nothing inside (practically, a null surface). - Empty, - /// The main surface of a [`DockState`](crate::DockState), only one should exist at surface index 0 at any one time. Main(Tree), @@ -17,26 +14,19 @@ pub enum Surface { } impl Surface { - /// Is this surface [`Empty`](Self::Empty) (in practice null)? - pub const fn is_empty(&self) -> bool { - matches!(self, Self::Empty) - } - /// Get access to the node tree of this surface. - pub fn node_tree(&self) -> Option<&Tree> { + pub fn node_tree(&self) -> &Tree { match self { - Surface::Empty => None, - Surface::Main(tree) => Some(tree), - Surface::Window(tree, _) => Some(tree), + Surface::Main(tree) => tree, + Surface::Window(tree, _) => tree, } } /// Get mutable access to the node tree of this surface. - pub fn node_tree_mut(&mut self) -> Option<&mut Tree> { + pub fn node_tree_mut(&mut self) -> &mut Tree { match self { - Surface::Empty => None, - Surface::Main(tree) => Some(tree), - Surface::Window(tree, _) => Some(tree), + Surface::Main(tree) => tree, + Surface::Window(tree, _) => tree, } } @@ -44,20 +34,14 @@ impl Surface { /// /// If the surface is [`Empty`](Self::Empty), then the returned [`Iterator`] will be empty. pub fn iter_nodes(&self) -> impl Iterator> { - match self.node_tree() { - Some(tree) => tree.iter(), - None => todo!(), - } + self.node_tree().iter() } /// Returns a mutable [`Iterator`] of nodes in this surface's tree. /// /// If the surface is [`Empty`](Self::Empty), then the returned [`Iterator`] will be empty. pub fn iter_nodes_mut(&mut self) -> impl Iterator> { - match self.node_tree_mut() { - Some(tree) => tree.iter_mut(), - None => todo!(), - } + self.node_tree_mut().iter_mut() } /// Returns an [`Iterator`] of **all** tabs in this surface's tree, @@ -84,15 +68,9 @@ impl Surface { F: Clone + FnMut(&Tab) -> Option, { match self { - Surface::Empty => Surface::Empty, Surface::Main(tree) => Surface::Main(tree.filter_map_tabs(function)), Surface::Window(tree, window_state) => { - let tree = tree.filter_map_tabs(function); - if tree.is_empty() { - Surface::Empty - } else { - Surface::Window(tree, window_state.clone()) - } + Surface::Window(tree.filter_map_tabs(function), window_state.clone()) } } } @@ -123,11 +101,7 @@ impl Surface { where F: Clone + FnMut(&mut Tab) -> bool, { - if let Surface::Main(tree) | Surface::Window(tree, _) = self { - tree.retain_tabs(predicate); - if tree.is_empty() { - *self = Surface::Empty; - } - } + let (Surface::Main(tree) | Surface::Window(tree, _)) = self; + tree.retain_tabs(predicate); } }