From d49892431b9cde5c243a9aa199d30c901cc66d1c Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Wed, 19 May 2021 21:27:53 -0700 Subject: [PATCH] matchers: make visit() return cloned sets instead of references This is just to avoid the lifetime parameter. It was a premature optimization to return a reference (we don't even use the matchers yet, so it cloning these sets clearly doesn't show up in profiling). --- lib/src/matchers.rs | 60 ++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/lib/src/matchers.rs b/lib/src/matchers.rs index d0274394a..8eb0a8260 100644 --- a/lib/src/matchers.rs +++ b/lib/src/matchers.rs @@ -19,21 +19,21 @@ use std::collections::{HashMap, HashSet}; use crate::repo_path::{RepoPath, RepoPathComponent}; #[derive(PartialEq, Eq, Debug)] -pub struct Visit<'matcher> { - pub dirs: VisitDirs<'matcher>, - pub files: VisitFiles<'matcher>, +pub struct Visit { + dirs: VisitDirs, + files: VisitFiles, } #[derive(PartialEq, Eq, Debug)] -pub enum VisitDirs<'matcher> { +pub enum VisitDirs { All, - Set(&'matcher HashSet), + Set(HashSet), } #[derive(PartialEq, Eq, Debug)] -pub enum VisitFiles<'matcher> { +pub enum VisitFiles { All, - Set(&'matcher HashSet), + Set(HashSet), } pub trait Matcher { @@ -94,8 +94,6 @@ impl Matcher for FilesMatcher { struct Dirs { dirs: HashMap>, files: HashMap>, - empty_dirs: HashSet, - empty_files: HashSet, } impl Dirs { @@ -103,8 +101,6 @@ impl Dirs { Dirs { dirs: HashMap::new(), files: HashMap::new(), - empty_dirs: HashSet::new(), - empty_files: HashSet::new(), } } @@ -137,33 +133,31 @@ impl Dirs { self.files.entry(dir).or_default().insert(basename.clone()); } - fn get_dirs(&self, dir: &RepoPath) -> &HashSet { - self.dirs.get(dir).unwrap_or(&self.empty_dirs) + fn get_dirs(&self, dir: &RepoPath) -> HashSet { + self.dirs.get(dir).cloned().unwrap_or_default() } - fn get_files(&self, dir: &RepoPath) -> &HashSet { - self.files.get(dir).unwrap_or(&self.empty_files) + fn get_files(&self, dir: &RepoPath) -> HashSet { + self.files.get(dir).cloned().unwrap_or_default() } } #[cfg(test)] mod tests { - use std::collections::HashSet; - use super::*; use crate::repo_path::{RepoPath, RepoPathComponent}; #[test] fn dirs_empty() { let dirs = Dirs::new(); - assert_eq!(dirs.get_dirs(&RepoPath::root()), &hashset! {}); + assert_eq!(dirs.get_dirs(&RepoPath::root()), hashset! {}); } #[test] fn dirs_root() { let mut dirs = Dirs::new(); dirs.add_dir(RepoPath::root()); - assert_eq!(dirs.get_dirs(&RepoPath::root()), &hashset! {}); + assert_eq!(dirs.get_dirs(&RepoPath::root()), hashset! {}); } #[test] @@ -172,7 +166,7 @@ mod tests { dirs.add_dir(RepoPath::from_internal_string("dir")); assert_eq!( dirs.get_dirs(&RepoPath::root()), - &hashset! {RepoPathComponent::from("dir")} + hashset! {RepoPathComponent::from("dir")} ); } @@ -182,21 +176,21 @@ mod tests { dirs.add_file(&RepoPath::from_internal_string("dir/file")); assert_eq!( dirs.get_dirs(&RepoPath::root()), - &hashset! {RepoPathComponent::from("dir")} + hashset! {RepoPathComponent::from("dir")} ); - assert_eq!(dirs.get_files(&RepoPath::root()), &hashset! {}); + assert_eq!(dirs.get_files(&RepoPath::root()), hashset! {}); } #[test] fn filesmatcher_empty() { - let m = FilesMatcher::new(HashSet::new()); + let m = FilesMatcher::new(hashset! {}); assert!(!m.matches(&RepoPath::from_internal_string("file"))); assert!(!m.matches(&RepoPath::from_internal_string("dir/file"))); assert_eq!( m.visit(&RepoPath::root()), Visit { - dirs: VisitDirs::Set(&HashSet::new()), - files: VisitFiles::Set(&HashSet::new()), + dirs: VisitDirs::Set(hashset! {}), + files: VisitFiles::Set(hashset! {}), } ); } @@ -213,33 +207,33 @@ mod tests { assert_eq!( m.visit(&RepoPath::root()), Visit { - dirs: VisitDirs::Set(&hashset! {RepoPathComponent::from("dir1")}), - files: VisitFiles::Set(&hashset! {RepoPathComponent::from("file4")}), + dirs: VisitDirs::Set(hashset! {RepoPathComponent::from("dir1")}), + files: VisitFiles::Set(hashset! {RepoPathComponent::from("file4")}), } ); assert_eq!( m.visit(&RepoPath::from_internal_string("dir1")), Visit { dirs: VisitDirs::Set( - &hashset! {RepoPathComponent::from("subdir1"), RepoPathComponent::from("subdir2")} + hashset! {RepoPathComponent::from("subdir1"), RepoPathComponent::from("subdir2")} ), - files: VisitFiles::Set(&hashset! {}), + files: VisitFiles::Set(hashset! {}), } ); assert_eq!( m.visit(&RepoPath::from_internal_string("dir1/subdir1")), Visit { - dirs: VisitDirs::Set(&hashset! {}), + dirs: VisitDirs::Set(hashset! {}), files: VisitFiles::Set( - &hashset! {RepoPathComponent::from("file1"), RepoPathComponent::from("file2")} + hashset! {RepoPathComponent::from("file1"), RepoPathComponent::from("file2")} ), } ); assert_eq!( m.visit(&RepoPath::from_internal_string("dir1/subdir2")), Visit { - dirs: VisitDirs::Set(&hashset! {}), - files: VisitFiles::Set(&hashset! {RepoPathComponent::from("file3")}), + dirs: VisitDirs::Set(hashset! {}), + files: VisitFiles::Set(hashset! {RepoPathComponent::from("file3")}), } ); }