##// END OF EJS Templates
rust-dirs-multiset: use `AsRef` instead of concrete types when possible...
Raphaël Gomès -
r44283:088ba9d9 default
parent child Browse files
Show More
@@ -28,14 +28,14 b' impl DirsMultiset {'
28 28 ///
29 29 /// If `skip_state` is provided, skips dirstate entries with equal state.
30 30 pub fn from_dirstate(
31 vec: &FastHashMap<HgPathBuf, DirstateEntry>,
31 dirstate: &FastHashMap<HgPathBuf, DirstateEntry>,
32 32 skip_state: Option<EntryState>,
33 33 ) -> Self {
34 34 let mut multiset = DirsMultiset {
35 35 inner: FastHashMap::default(),
36 36 };
37 37
38 for (filename, DirstateEntry { state, .. }) in vec {
38 for (filename, DirstateEntry { state, .. }) in dirstate {
39 39 // This `if` is optimized out of the loop
40 40 if let Some(skip) = skip_state {
41 41 if skip != *state {
@@ -50,13 +50,13 b' impl DirsMultiset {'
50 50 }
51 51
52 52 /// Initializes the multiset from a manifest.
53 pub fn from_manifest(vec: &Vec<HgPathBuf>) -> Self {
53 pub fn from_manifest(manifest: &[impl AsRef<HgPath>]) -> Self {
54 54 let mut multiset = DirsMultiset {
55 55 inner: FastHashMap::default(),
56 56 };
57 57
58 for filename in vec {
59 multiset.add_path(filename);
58 for filename in manifest {
59 multiset.add_path(filename.as_ref());
60 60 }
61 61
62 62 multiset
@@ -65,8 +65,11 b' impl DirsMultiset {'
65 65 /// Increases the count of deepest directory contained in the path.
66 66 ///
67 67 /// If the directory is not yet in the map, adds its parents.
68 pub fn add_path(&mut self, path: &HgPath) -> Result<(), DirstateMapError> {
69 for subpath in files::find_dirs(path) {
68 pub fn add_path(
69 &mut self,
70 path: impl AsRef<HgPath>,
71 ) -> Result<(), DirstateMapError> {
72 for subpath in files::find_dirs(path.as_ref()) {
70 73 if subpath.as_bytes().last() == Some(&b'/') {
71 74 // TODO Remove this once PathAuditor is certified
72 75 // as the only entrypoint for path data
@@ -88,9 +91,9 b' impl DirsMultiset {'
88 91 /// If the directory is not in the map, something horrible has happened.
89 92 pub fn delete_path(
90 93 &mut self,
91 path: &HgPath,
94 path: impl AsRef<HgPath>,
92 95 ) -> Result<(), DirstateMapError> {
93 for subpath in files::find_dirs(path) {
96 for subpath in files::find_dirs(path.as_ref()) {
94 97 match self.inner.entry(subpath.to_owned()) {
95 98 Entry::Occupied(mut entry) => {
96 99 let val = entry.get().clone();
@@ -102,7 +105,7 b' impl DirsMultiset {'
102 105 }
103 106 Entry::Vacant(_) => {
104 107 return Err(DirstateMapError::PathNotFound(
105 path.to_owned(),
108 path.as_ref().to_owned(),
106 109 ))
107 110 }
108 111 };
@@ -111,8 +114,8 b' impl DirsMultiset {'
111 114 Ok(())
112 115 }
113 116
114 pub fn contains(&self, key: &HgPath) -> bool {
115 self.inner.contains_key(key)
117 pub fn contains(&self, key: impl AsRef<HgPath>) -> bool {
118 self.inner.contains_key(key.as_ref())
116 119 }
117 120
118 121 pub fn iter(&self) -> DirsMultisetIter {
@@ -130,7 +133,8 b' mod tests {'
130 133
131 134 #[test]
132 135 fn test_delete_path_path_not_found() {
133 let mut map = DirsMultiset::from_manifest(&vec![]);
136 let manifest: Vec<HgPathBuf> = vec![];
137 let mut map = DirsMultiset::from_manifest(&manifest);
134 138 let path = HgPathBuf::from_bytes(b"doesnotexist/");
135 139 assert_eq!(
136 140 Err(DirstateMapError::PathNotFound(path.to_owned())),
@@ -186,7 +190,8 b' mod tests {'
186 190
187 191 #[test]
188 192 fn test_add_path_empty_path() {
189 let mut map = DirsMultiset::from_manifest(&vec![]);
193 let manifest: Vec<HgPathBuf> = vec![];
194 let mut map = DirsMultiset::from_manifest(&manifest);
190 195 let path = HgPath::new(b"");
191 196 map.add_path(path);
192 197
@@ -195,7 +200,8 b' mod tests {'
195 200
196 201 #[test]
197 202 fn test_add_path_successful() {
198 let mut map = DirsMultiset::from_manifest(&vec![]);
203 let manifest: Vec<HgPathBuf> = vec![];
204 let mut map = DirsMultiset::from_manifest(&manifest);
199 205
200 206 map.add_path(HgPath::new(b"a/"));
201 207 assert_eq!(1, *map.inner.get(HgPath::new(b"a")).unwrap());
@@ -240,7 +246,8 b' mod tests {'
240 246
241 247 #[test]
242 248 fn test_dirsmultiset_new_empty() {
243 let new = DirsMultiset::from_manifest(&vec![]);
249 let manifest: Vec<HgPathBuf> = vec![];
250 let new = DirsMultiset::from_manifest(&manifest);
244 251 let expected = DirsMultiset {
245 252 inner: FastHashMap::default(),
246 253 };
@@ -255,7 +262,7 b' mod tests {'
255 262
256 263 #[test]
257 264 fn test_dirsmultiset_new_no_skip() {
258 let input_vec = ["a/", "b/", "a/c", "a/d/"]
265 let input_vec: Vec<HgPathBuf> = ["a/", "b/", "a/c", "a/d/"]
259 266 .iter()
260 267 .map(|e| HgPathBuf::from_bytes(e.as_bytes()))
261 268 .collect();
General Comments 0
You need to be logged in to leave comments. Login now