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