about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-10-23 08:42:21 -0700
committerAlex Crichton <alex@alexcrichton.com>2014-10-30 08:54:30 -0700
commit1d356624a1c03363be37886ffdad7dcf25ee81f6 (patch)
tree4aef71be9e3f509b1bc8b4880e6894656e6404c4 /src/libstd
parent18a3db6aa1ce9e66b0c9cb776588d56470c6078b (diff)
downloadrust-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')
-rw-r--r--src/libstd/collections/hashmap/map.rs32
-rw-r--r--src/libstd/io/fs.rs2
-rw-r--r--src/libstd/num/strconv.rs4
-rw-r--r--src/libstd/path/windows.rs6
-rw-r--r--src/libstd/rand/mod.rs2
5 files changed, 21 insertions, 25 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> {
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<T:NumCast+Zero+One+PartialEq+PartialOrd+Float+
                     // or set to 0 if max and carry the 1.
                     let current_digit = ascii2value(buf[i as uint]);
                     if current_digit < (radix - 1) {
-                        *buf.get_mut(i as uint) = value2ascii(current_digit+1);
+                        buf[i as uint] = value2ascii(current_digit+1);
                         break;
                     } else {
-                        *buf.get_mut(i as uint) = value2ascii(0);
+                        buf[i as uint] = value2ascii(0);
                         i -= 1;
                     }
                 }
diff --git a/src/libstd/path/windows.rs b/src/libstd/path/windows.rs
index 1897c8638cc..1ddc027a07e 100644
--- a/src/libstd/path/windows.rs
+++ b/src/libstd/path/windows.rs
@@ -776,7 +776,7 @@ impl Path {
                                 let mut s = String::from_str(s.slice_to(len));
                                 unsafe {
                                     let v = s.as_mut_vec();
-                                    *v.get_mut(0) = (*v)[0]
+                                    v[0] = (*v)[0]
                                                      .to_ascii()
                                                      .to_uppercase()
                                                      .to_byte();
@@ -784,7 +784,7 @@ impl Path {
                                 if is_abs {
                                     // normalize C:/ to C:\
                                     unsafe {
-                                        *s.as_mut_vec().get_mut(2) = SEP_BYTE;
+                                        s.as_mut_vec()[2] = SEP_BYTE;
                                     }
                                 }
                                 Some(s)
@@ -794,7 +794,7 @@ impl Path {
                                 let mut s = String::from_str(s.slice_to(len));
                                 unsafe {
                                     let v = s.as_mut_vec();
-                                    *v.get_mut(4) = (*v)[4].to_ascii().to_uppercase().to_byte();
+                                    v[4] = (*v)[4].to_ascii().to_uppercase().to_byte();
                                 }
                                 Some(s)
                             }
diff --git a/src/libstd/rand/mod.rs b/src/libstd/rand/mod.rs
index d1c655cb4d0..21e531d211a 100644
--- a/src/libstd/rand/mod.rs
+++ b/src/libstd/rand/mod.rs
@@ -411,7 +411,7 @@ pub fn sample<T, I: Iterator<T>, 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;