From f3a709f36705ab1898d79989b14f3504606b007e Mon Sep 17 00:00:00 2001 From: Ved_s Date: Thu, 4 Jul 2024 06:44:32 +1100 Subject: [PATCH] Remove `Clone` bound on `map_tabs` and `filter_map_tabs` (#241) * remove Clone bound on map_tabs and filter_map_tabs * remove Clone bound from all functions with Clone + FnMut bounds * Update changelog * Add missing period in changelog --------- Co-authored-by: Adanos020 --- CHANGELOG.md | 7 +++++++ src/dock_state/mod.rs | 16 ++++++++-------- src/dock_state/surface.rs | 8 ++++---- src/dock_state/tree/mod.rs | 16 ++++++++-------- src/dock_state/tree/node.rs | 4 ++-- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1653f307..be3e4446 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # egui_dock changelog +## 0.14.0 - Unreleased + +## Changed + +- `{DockState,Surface,Tree,Node}::{filter_map_tabs,map_tabs,filter_tabs,retain_tabs}` no longer require the predicate to + implement `Clone`. ([#241](https://github.com/Adanos020/egui_dock/pull/241)) + ## 0.13.0 - 2024-07-03 ### Breaking changes diff --git a/src/dock_state/mod.rs b/src/dock_state/mod.rs index 404526d8..c603a92f 100644 --- a/src/dock_state/mod.rs +++ b/src/dock_state/mod.rs @@ -469,9 +469,9 @@ impl DockState { /// let tabs: Vec<_> = mapped_dock_state.iter_all_tabs().map(|(_, tab)| tab.to_owned()).collect(); /// assert_eq!(tabs, vec!["1".to_string(), "3".to_string()]); /// ``` - pub fn filter_map_tabs(&self, function: F) -> DockState + pub fn filter_map_tabs(&self, mut function: F) -> DockState where - F: Clone + FnMut(&Tab) -> Option, + F: FnMut(&Tab) -> Option, { let DockState { surfaces, @@ -481,7 +481,7 @@ impl DockState { let surfaces = surfaces .iter() .filter_map(|surface| { - let surface = surface.filter_map_tabs(function.clone()); + let surface = surface.filter_map_tabs(&mut function); (!surface.is_empty()).then_some(surface) }) .collect(); @@ -504,7 +504,7 @@ impl DockState { /// ``` pub fn map_tabs(&self, mut function: F) -> DockState where - F: Clone + FnMut(&Tab) -> NewTab, + F: FnMut(&Tab) -> NewTab, { self.filter_map_tabs(move |tab| Some(function(tab))) } @@ -522,7 +522,7 @@ impl DockState { /// ``` pub fn filter_tabs(&self, mut predicate: F) -> DockState where - F: Clone + FnMut(&Tab) -> bool, + F: FnMut(&Tab) -> bool, Tab: Clone, { self.filter_map_tabs(move |tab| predicate(tab).then(|| tab.clone())) @@ -539,12 +539,12 @@ impl DockState { /// let tabs: Vec<_> = dock_state.iter_all_tabs().map(|(_, tab)| tab.to_owned()).collect(); /// assert_eq!(tabs, vec!["tab1".to_string(), "tab2".to_string()]); /// ``` - pub fn retain_tabs(&mut self, predicate: F) + pub fn retain_tabs(&mut self, mut predicate: F) where - F: Clone + FnMut(&mut Tab) -> bool, + F: FnMut(&mut Tab) -> bool, { self.surfaces.retain_mut(|surface| { - surface.retain_tabs(predicate.clone()); + surface.retain_tabs(&mut predicate); !surface.is_empty() }); } diff --git a/src/dock_state/surface.rs b/src/dock_state/surface.rs index 476a7a7f..66da3b97 100644 --- a/src/dock_state/surface.rs +++ b/src/dock_state/surface.rs @@ -81,7 +81,7 @@ impl Surface { /// it'll change to [`Surface::Empty`]. pub fn filter_map_tabs(&self, function: F) -> Surface where - F: Clone + FnMut(&Tab) -> Option, + F: FnMut(&Tab) -> Option, { match self { Surface::Empty => Surface::Empty, @@ -100,7 +100,7 @@ impl Surface { /// Returns a new [`Surface`] while mapping the tab type. pub fn map_tabs(&self, mut function: F) -> Surface where - F: Clone + FnMut(&Tab) -> NewTab, + F: FnMut(&Tab) -> NewTab, { self.filter_map_tabs(move |tab| Some(function(tab))) } @@ -110,7 +110,7 @@ impl Surface { /// it'll change to [`Surface::Empty`]. pub fn filter_tabs(&self, mut predicate: F) -> Surface where - F: Clone + FnMut(&Tab) -> bool, + F: FnMut(&Tab) -> bool, Tab: Clone, { self.filter_map_tabs(move |tab| predicate(tab).then(|| tab.clone())) @@ -121,7 +121,7 @@ impl Surface { /// it'll change to [`Surface::Empty`]. pub fn retain_tabs(&mut self, predicate: F) where - F: Clone + FnMut(&mut Tab) -> bool, + F: FnMut(&mut Tab) -> bool, { if let Surface::Main(tree) | Surface::Window(tree, _) = self { tree.retain_tabs(predicate); diff --git a/src/dock_state/tree/mod.rs b/src/dock_state/tree/mod.rs index cdb6f3e5..648bc52c 100644 --- a/src/dock_state/tree/mod.rs +++ b/src/dock_state/tree/mod.rs @@ -737,9 +737,9 @@ impl Tree { /// Returns a new [`Tree`] while mapping and filtering the tab type. /// Any remaining empty [`Node`]s are removed. - pub fn filter_map_tabs(&self, function: F) -> Tree + pub fn filter_map_tabs(&self, mut function: F) -> Tree where - F: Clone + FnMut(&Tab) -> Option, + F: FnMut(&Tab) -> Option, { let Tree { focused_node, @@ -750,7 +750,7 @@ impl Tree { .iter() .enumerate() .map(|(index, node)| { - let filtered_node = node.filter_map_tabs(function.clone()); + let filtered_node = node.filter_map_tabs(&mut function); if filtered_node.is_empty() && !node.is_empty() { emptied_nodes.insert(NodeIndex(index)); } @@ -768,7 +768,7 @@ impl Tree { /// Returns a new [`Tree`] while mapping the tab type. pub fn map_tabs(&self, mut function: F) -> Tree where - F: Clone + FnMut(&Tab) -> NewTab, + F: FnMut(&Tab) -> NewTab, { self.filter_map_tabs(move |tab| Some(function(tab))) } @@ -777,7 +777,7 @@ impl Tree { /// Any remaining empty [`Node`]s are removed. pub fn filter_tabs(&self, mut predicate: F) -> Tree where - F: Clone + FnMut(&Tab) -> bool, + F: FnMut(&Tab) -> bool, Tab: Clone, { self.filter_map_tabs(move |tab| predicate(tab).then(|| tab.clone())) @@ -785,13 +785,13 @@ impl Tree { /// Removes all tabs for which `predicate` returns `false`. /// Any remaining empty [`Node`]s are also removed. - pub fn retain_tabs(&mut self, predicate: F) + pub fn retain_tabs(&mut self, mut predicate: F) where - F: Clone + FnMut(&mut Tab) -> bool, + F: FnMut(&mut Tab) -> bool, { let mut emptied_nodes = HashSet::default(); for (index, node) in self.nodes.iter_mut().enumerate() { - node.retain_tabs(predicate.clone()); + node.retain_tabs(&mut predicate); if node.is_empty() { emptied_nodes.insert(NodeIndex(index)); } diff --git a/src/dock_state/tree/node.rs b/src/dock_state/tree/node.rs index 26958e89..113a9032 100644 --- a/src/dock_state/tree/node.rs +++ b/src/dock_state/tree/node.rs @@ -340,7 +340,7 @@ impl Node { /// If this [`Node`] remains empty, it will change to [`Node::Empty`]. pub fn filter_tabs(&self, mut predicate: F) -> Node where - F: Clone + FnMut(&Tab) -> bool, + F: FnMut(&Tab) -> bool, Tab: Clone, { self.filter_map_tabs(move |tab| predicate(tab).then(|| tab.clone())) @@ -350,7 +350,7 @@ impl Node { /// If this [`Node`] remains empty, it will change to [`Node::Empty`]. pub fn retain_tabs(&mut self, predicate: F) where - F: Clone + FnMut(&mut Tab) -> bool, + F: FnMut(&mut Tab) -> bool, { if let Node::Leaf { tabs, .. } = self { tabs.retain_mut(predicate);