diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2014-10-23 08:42:21 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2014-10-30 08:54:30 -0700 |
| commit | 1d356624a1c03363be37886ffdad7dcf25ee81f6 (patch) | |
| tree | 4aef71be9e3f509b1bc8b4880e6894656e6404c4 /src/libstd/collections | |
| parent | 18a3db6aa1ce9e66b0c9cb776588d56470c6078b (diff) | |
| download | rust-1d356624a1c03363be37886ffdad7dcf25ee81f6.tar.gz rust-1d356624a1c03363be37886ffdad7dcf25ee81f6.zip | |
collections: Enable IndexMut for some collections
This commit enables implementations of IndexMut for a number of collections, including Vec, RingBuf, SmallIntMap, TrieMap, TreeMap, and HashMap. At the same time this deprecates the `get_mut` methods on vectors in favor of using the indexing notation. cc #18424
Diffstat (limited to 'src/libstd/collections')
| -rw-r--r-- | src/libstd/collections/hashmap/map.rs | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/src/libstd/collections/hashmap/map.rs b/src/libstd/collections/hashmap/map.rs index 6562a644988..cb47c28f8be 100644 --- a/src/libstd/collections/hashmap/map.rs +++ b/src/libstd/collections/hashmap/map.rs @@ -14,19 +14,14 @@ use clone::Clone; use cmp::{max, Eq, Equiv, PartialEq}; use collections::{Collection, Mutable, MutableSet, Map, MutableMap}; use default::Default; -use fmt::Show; -use fmt; +use fmt::{mod, Show}; use hash::{Hash, Hasher, RandomSipHasher}; -use iter::{Iterator, FromIterator, Extendable}; -use iter; -use mem::replace; -use mem; +use iter::{mod, Iterator, FromIterator, Extendable}; +use mem::{mod, replace}; use num; -use ops::Deref; +use ops::{Deref, Index, IndexMut}; use option::{Some, None, Option}; -use result::{Ok, Err}; -use ops::Index; -use core::result::Result; +use result::{Result, Ok, Err}; use super::table; use super::table::{ @@ -837,6 +832,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> { /// # Example /// /// ``` + /// # #![allow(deprecated)] /// use std::collections::HashMap; /// /// let mut map = HashMap::new(); @@ -852,11 +848,9 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> { /// *map.get_mut(&"a") = -2; /// assert_eq!(map["a"], -2); /// ``` + #[deprecated = "use indexing instead: `&mut map[key]`"] pub fn get_mut<'a>(&'a mut self, k: &K) -> &'a mut V { - match self.find_mut(k) { - Some(v) => v, - None => panic!("no entry found for key") - } + &mut self[*k] } /// Return true if the map contains a value for the specified key, @@ -1194,13 +1188,15 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> Index<K, V> for HashMap<K, V, H> { } } -// FIXME(#12825) Indexing will always try IndexMut first and that causes issues. -/*impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> ops::IndexMut<K, V> for HashMap<K, V, H> { +impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> IndexMut<K, V> for HashMap<K, V, H> { #[inline] fn index_mut<'a>(&'a mut self, index: &K) -> &'a mut V { - self.get_mut(index) + match self.find_mut(index) { + Some(v) => v, + None => panic!("no entry found for key") + } } -}*/ +} /// HashMap iterator pub struct Entries<'a, K: 'a, V: 'a> { |
