diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2015-03-21 19:33:27 -0400 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2015-03-23 16:55:43 -0400 |
| commit | b4d4daf007753dfb04d87b1ffe1c2ad2d8811d5a (patch) | |
| tree | c35d3d74c271c4ec72678d9d866bcad331a9e706 /src/libstd/collections | |
| parent | bc1dde468c1613743c919cb9f33923cc9916c5b4 (diff) | |
| download | rust-b4d4daf007753dfb04d87b1ffe1c2ad2d8811d5a.tar.gz rust-b4d4daf007753dfb04d87b1ffe1c2ad2d8811d5a.zip | |
Adjust Index/IndexMut impls. For generic collections, we take
references. For collections whose keys are integers, we take both references and by-value.
Diffstat (limited to 'src/libstd/collections')
| -rw-r--r-- | src/libstd/collections/hash/map.rs | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 9139e182ce4..86664d7eb0c 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -1088,7 +1088,7 @@ impl<K, V, S> HashMap<K, V, S> /// Some(x) => *x = "b", /// None => (), /// } - /// assert_eq!(map[1], "b"); + /// assert_eq!(map[&1], "b"); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V> @@ -1111,7 +1111,7 @@ impl<K, V, S> HashMap<K, V, S> /// /// map.insert(37, "b"); /// assert_eq!(map.insert(37, "c"), Some("b")); - /// assert_eq!(map[37], "c"); + /// assert_eq!(map[&37], "c"); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn insert(&mut self, k: K, v: V) -> Option<V> { @@ -1244,6 +1244,7 @@ impl<K, V, S> Default for HashMap<K, V, S> } } +#[cfg(stage0)] #[stable(feature = "rust1", since = "1.0.0")] impl<K, Q: ?Sized, V, S> Index<Q> for HashMap<K, V, S> where K: Eq + Hash + Borrow<Q>, @@ -1258,6 +1259,21 @@ impl<K, Q: ?Sized, V, S> Index<Q> for HashMap<K, V, S> } } +#[cfg(not(stage0))] +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a, K, Q: ?Sized, V, S> Index<&'a Q> for HashMap<K, V, S> + where K: Eq + Hash + Borrow<Q>, + Q: Eq + Hash, + S: HashState, +{ + type Output = V; + + #[inline] + fn index(&self, index: &Q) -> &V { + self.get(index).expect("no entry found for key") + } +} + /// HashMap iterator. #[stable(feature = "rust1", since = "1.0.0")] pub struct Iter<'a, K: 'a, V: 'a> { @@ -2185,7 +2201,7 @@ mod test_map { map.insert(2, 1); map.insert(3, 4); - assert_eq!(map[2], 1); + assert_eq!(map[&2], 1); } #[test] @@ -2197,7 +2213,7 @@ mod test_map { map.insert(2, 1); map.insert(3, 4); - map[4]; + map[&4]; } #[test] |
