From 1d356624a1c03363be37886ffdad7dcf25ee81f6 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 23 Oct 2014 08:42:21 -0700 Subject: 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 --- src/libstd/collections/hashmap/map.rs | 32 ++++++++++++++------------------ src/libstd/io/fs.rs | 2 +- src/libstd/num/strconv.rs | 4 ++-- src/libstd/path/windows.rs | 6 +++--- src/libstd/rand/mod.rs | 2 +- 5 files changed, 21 insertions(+), 25 deletions(-) (limited to 'src/libstd') 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, V, S, H: Hasher> HashMap { /// # Example /// /// ``` + /// # #![allow(deprecated)] /// use std::collections::HashMap; /// /// let mut map = HashMap::new(); @@ -852,11 +848,9 @@ impl, V, S, H: Hasher> HashMap { /// *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, V, S, H: Hasher> Index for HashMap { } } -// FIXME(#12825) Indexing will always try IndexMut first and that causes issues. -/*impl, V, S, H: Hasher> ops::IndexMut for HashMap { +impl, V, S, H: Hasher> IndexMut for HashMap { #[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> { diff --git a/src/libstd/io/fs.rs b/src/libstd/io/fs.rs index f749d6c823e..f193ce8cffa 100644 --- a/src/libstd/io/fs.rs +++ b/src/libstd/io/fs.rs @@ -963,7 +963,7 @@ mod test { macro_rules! error( ($e:expr, $s:expr) => ( match $e { - Ok(val) => panic!("Unexpected success. Should've been: {}", $s), + Ok(_) => panic!("Unexpected success. Should've been: {}", $s), Err(ref err) => assert!(err.to_string().as_slice().contains($s.as_slice()), format!("`{}` did not contain `{}`", err, $s)) } diff --git a/src/libstd/num/strconv.rs b/src/libstd/num/strconv.rs index 6e0d81a63c9..f79cda0195e 100644 --- a/src/libstd/num/strconv.rs +++ b/src/libstd/num/strconv.rs @@ -424,10 +424,10 @@ pub fn float_to_str_bytes_common, R: Rng>(rng: &mut R, for (i, elem) in iter.enumerate() { let k = rng.gen_range(0, i + 1 + amount); if k < amount { - *reservoir.get_mut(k) = elem; + reservoir[k] = elem; } } return reservoir; -- cgit 1.4.1-3-g733a5