##// END OF EJS Templates
rust: remove Deref in favor of explicit methods...
Raphaël Gomès -
r42762:a80464e8 default
parent child Browse files
Show More
@@ -8,9 +8,8 b''
8 //! A multiset of directory names.
8 //! A multiset of directory names.
9 //!
9 //!
10 //! Used to counts the references to directories in a manifest or dirstate.
10 //! Used to counts the references to directories in a manifest or dirstate.
11 use std::collections::hash_map::Entry;
11 use std::collections::hash_map::{Entry, Iter};
12 use std::collections::HashMap;
12 use std::collections::HashMap;
13 use std::ops::Deref;
14 use {DirsIterable, DirstateEntry, DirstateMapError};
13 use {DirsIterable, DirstateEntry, DirstateMapError};
15
14
16 #[derive(PartialEq, Debug)]
15 #[derive(PartialEq, Debug)]
@@ -18,14 +17,6 b' pub struct DirsMultiset {'
18 inner: HashMap<Vec<u8>, u32>,
17 inner: HashMap<Vec<u8>, u32>,
19 }
18 }
20
19
21 impl Deref for DirsMultiset {
22 type Target = HashMap<Vec<u8>, u32>;
23
24 fn deref(&self) -> &Self::Target {
25 &self.inner
26 }
27 }
28
29 impl DirsMultiset {
20 impl DirsMultiset {
30 /// Initializes the multiset from a dirstate or a manifest.
21 /// Initializes the multiset from a dirstate or a manifest.
31 ///
22 ///
@@ -132,6 +123,18 b' impl DirsMultiset {'
132
123
133 Ok(())
124 Ok(())
134 }
125 }
126
127 pub fn contains_key(&self, key: &[u8]) -> bool {
128 self.inner.contains_key(key)
129 }
130
131 pub fn iter(&self) -> Iter<Vec<u8>, u32> {
132 self.inner.iter()
133 }
134
135 pub fn len(&self) -> usize {
136 self.inner.len()
137 }
135 }
138 }
136
139
137 #[cfg(test)]
140 #[cfg(test)]
@@ -176,8 +179,8 b' mod tests {'
176 map.delete_path(b"a/b/")
179 map.delete_path(b"a/b/")
177 );
180 );
178
181
179 assert_eq!(2, *map.get(&b"a".to_vec()).unwrap());
182 assert_eq!(2, *map.inner.get(&b"a".to_vec()).unwrap());
180 assert_eq!(1, *map.get(&b"a/c".to_vec()).unwrap());
183 assert_eq!(1, *map.inner.get(&b"a/c".to_vec()).unwrap());
181 eprintln!("{:?}", map);
184 eprintln!("{:?}", map);
182 assert_eq!(Ok(()), map.delete_path(b"a/"));
185 assert_eq!(Ok(()), map.delete_path(b"a/"));
183 eprintln!("{:?}", map);
186 eprintln!("{:?}", map);
@@ -203,36 +206,36 b' mod tests {'
203 let mut map = DirsMultiset::new(DirsIterable::Manifest(vec![]), None);
206 let mut map = DirsMultiset::new(DirsIterable::Manifest(vec![]), None);
204
207
205 map.add_path(b"a/");
208 map.add_path(b"a/");
206 assert_eq!(1, *map.get(&b"a".to_vec()).unwrap());
209 assert_eq!(1, *map.inner.get(&b"a".to_vec()).unwrap());
207 assert_eq!(1, *map.get(&Vec::new()).unwrap());
210 assert_eq!(1, *map.inner.get(&Vec::new()).unwrap());
208 assert_eq!(2, map.len());
211 assert_eq!(2, map.len());
209
212
210 // Non directory should be ignored
213 // Non directory should be ignored
211 map.add_path(b"a");
214 map.add_path(b"a");
212 assert_eq!(1, *map.get(&b"a".to_vec()).unwrap());
215 assert_eq!(1, *map.inner.get(&b"a".to_vec()).unwrap());
213 assert_eq!(2, map.len());
216 assert_eq!(2, map.len());
214
217
215 // Non directory will still add its base
218 // Non directory will still add its base
216 map.add_path(b"a/b");
219 map.add_path(b"a/b");
217 assert_eq!(2, *map.get(&b"a".to_vec()).unwrap());
220 assert_eq!(2, *map.inner.get(&b"a".to_vec()).unwrap());
218 assert_eq!(2, map.len());
221 assert_eq!(2, map.len());
219
222
220 // Duplicate path works
223 // Duplicate path works
221 map.add_path(b"a/");
224 map.add_path(b"a/");
222 assert_eq!(3, *map.get(&b"a".to_vec()).unwrap());
225 assert_eq!(3, *map.inner.get(&b"a".to_vec()).unwrap());
223
226
224 // Nested dir adds to its base
227 // Nested dir adds to its base
225 map.add_path(b"a/b/");
228 map.add_path(b"a/b/");
226 assert_eq!(4, *map.get(&b"a".to_vec()).unwrap());
229 assert_eq!(4, *map.inner.get(&b"a".to_vec()).unwrap());
227 assert_eq!(1, *map.get(&b"a/b".to_vec()).unwrap());
230 assert_eq!(1, *map.inner.get(&b"a/b".to_vec()).unwrap());
228
231
229 // but not its base's base, because it already existed
232 // but not its base's base, because it already existed
230 map.add_path(b"a/b/c/");
233 map.add_path(b"a/b/c/");
231 assert_eq!(4, *map.get(&b"a".to_vec()).unwrap());
234 assert_eq!(4, *map.inner.get(&b"a".to_vec()).unwrap());
232 assert_eq!(2, *map.get(&b"a/b".to_vec()).unwrap());
235 assert_eq!(2, *map.inner.get(&b"a/b".to_vec()).unwrap());
233
236
234 map.add_path(b"a/c/");
237 map.add_path(b"a/c/");
235 assert_eq!(1, *map.get(&b"a/c".to_vec()).unwrap());
238 assert_eq!(1, *map.inner.get(&b"a/c".to_vec()).unwrap());
236
239
237 let expected = DirsMultiset {
240 let expected = DirsMultiset {
238 inner: [("", 2), ("a", 5), ("a/b", 2), ("a/b/c", 1), ("a/c", 1)]
241 inner: [("", 2), ("a", 5), ("a/b", 2), ("a/b/c", 1), ("a/c", 1)]
General Comments 0
You need to be logged in to leave comments. Login now