Skip to content

Commit

Permalink
perf: prefer into_iter() over clone()
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Feb 24, 2024
1 parent 4130a80 commit 86458ff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn get_opened_nothing_opened_is_top_level() {
let opened = HashSet::new();
let result = flatten(&opened, &items);
let result_text = result
.iter()
.into_iter()
.map(|flattened| flattened.item.identifier)
.collect::<Vec<_>>();
assert_eq!(result_text, ["a", "b", "h"]);
Expand All @@ -99,7 +99,7 @@ fn get_opened_wrong_opened_is_only_top_level() {
opened.insert(vec!["b", "d"]);
let result = flatten(&opened, &items);
let result_text = result
.iter()
.into_iter()
.map(|flattened| flattened.item.identifier)
.collect::<Vec<_>>();
assert_eq!(result_text, ["a", "b", "h"]);
Expand All @@ -112,7 +112,7 @@ fn get_opened_one_is_opened() {
opened.insert(vec!["b"]);
let result = flatten(&opened, &items);
let result_text = result
.iter()
.into_iter()
.map(|flattened| flattened.item.identifier)
.collect::<Vec<_>>();
assert_eq!(result_text, ["a", "b", "c", "d", "g", "h"]);
Expand All @@ -126,7 +126,7 @@ fn get_opened_all_opened() {
opened.insert(vec!["b", "d"]);
let result = flatten(&opened, &items);
let result_text = result
.iter()
.into_iter()
.map(|flattened| flattened.item.identifier)
.collect::<Vec<_>>();
assert_eq!(result_text, ["a", "b", "c", "d", "e", "f", "g", "h"]);
Expand Down
19 changes: 9 additions & 10 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ where
pub fn select_first(&mut self, items: &[Item<Identifier>]) -> bool {
let identifier = items
.first()
.map(|item| vec![item.identifier.clone()])
.unwrap_or_default();
.map_or(Vec::new(), |item| vec![item.identifier.clone()]);
self.select(identifier)
}

Expand All @@ -123,9 +122,9 @@ where
pub fn select_last(&mut self, items: &[Item<Identifier>]) -> bool {
let visible = self.flatten(items);
let new_identifier = visible
.into_iter()
.last()
.map(|flattened| flattened.identifier.clone())
.unwrap_or_default();
.map_or(Vec::new(), |flattened| flattened.identifier);
self.select(new_identifier)
}

Expand All @@ -138,9 +137,9 @@ where
let visible = self.flatten(items);
let new_index = new_index.min(visible.len().saturating_sub(1));
let new_identifier = visible
.get(new_index)
.map(|flattened| flattened.identifier.clone())
.unwrap_or_default();
.into_iter()
.nth(new_index)
.map_or(Vec::new(), |flattened| flattened.identifier);
self.select(new_identifier)
}

Expand Down Expand Up @@ -178,9 +177,9 @@ where
.position(|flattened| flattened.identifier == current_identifier);
let new_index = change_function(current_index).min(visible.len().saturating_sub(1));
let new_identifier = visible
.get(new_index)
.map(|flattened| flattened.identifier.clone())
.unwrap_or_default();
.into_iter()
.nth(new_index)
.map_or(Vec::new(), |flattened| flattened.identifier);
self.select(new_identifier)
}

Expand Down

0 comments on commit 86458ff

Please sign in to comment.