]> git.lizzy.rs Git - rust.git/blobdiff - src/libcollections/vec_map.rs
make `IndexMut` a super trait over `Index`
[rust.git] / src / libcollections / vec_map.rs
index 2846414bb9accd8df3c5fa838e2b6961c2fd873b..739b8d8ce19c24899abcd9d06f2a25d62d848623 100644 (file)
@@ -13,6 +13,8 @@
 
 #![allow(missing_docs)]
 
+use self::Entry::*;
+
 use core::prelude::*;
 
 use core::cmp::Ordering;
@@ -27,8 +29,6 @@
 use {vec, slice};
 use vec::Vec;
 
-// FIXME(conventions): capacity management???
-
 /// A map optimized for small integer keys.
 ///
 /// # Examples
@@ -66,6 +66,32 @@ pub struct VecMap<V> {
     v: Vec<Option<V>>,
 }
 
+/// A view into a single entry in a map, which may either be vacant or occupied.
+#[unstable(feature = "collections",
+           reason = "precise API still under development")]
+pub enum Entry<'a, V:'a> {
+    /// A vacant Entry
+    Vacant(VacantEntry<'a, V>),
+    /// An occupied Entry
+    Occupied(OccupiedEntry<'a, V>),
+}
+
+/// A vacant Entry.
+#[unstable(feature = "collections",
+           reason = "precise API still under development")]
+pub struct VacantEntry<'a, V:'a> {
+    map: &'a mut VecMap<V>,
+    index: usize,
+}
+
+/// An occupied Entry.
+#[unstable(feature = "collections",
+           reason = "precise API still under development")]
+pub struct OccupiedEntry<'a, V:'a> {
+    map: &'a mut VecMap<V>,
+    index: usize,
+}
+
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<V> Default for VecMap<V> {
     #[stable(feature = "rust1", since = "1.0.0")]
@@ -89,8 +115,8 @@ impl<S: Writer + Hasher, V: Hash<S>> Hash<S> for VecMap<V> {
     fn hash(&self, state: &mut S) {
         // In order to not traverse the `VecMap` twice, count the elements
         // during iteration.
-        let mut count: uint = 0;
-        for elt in self.iter() {
+        let mut count: usize = 0;
+        for elt in self {
             elt.hash(state);
             count += 1;
         }
@@ -120,7 +146,7 @@ pub fn new() -> VecMap<V> { VecMap { v: vec![] } }
     /// let mut map: VecMap<&str> = VecMap::with_capacity(10);
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
-    pub fn with_capacity(capacity: uint) -> VecMap<V> {
+    pub fn with_capacity(capacity: usize) -> VecMap<V> {
         VecMap { v: Vec::with_capacity(capacity) }
     }
 
@@ -136,7 +162,7 @@ pub fn with_capacity(capacity: uint) -> VecMap<V> {
     /// ```
     #[inline]
     #[stable(feature = "rust1", since = "1.0.0")]
-    pub fn capacity(&self) -> uint {
+    pub fn capacity(&self) -> usize {
         self.v.capacity()
     }
 
@@ -155,7 +181,7 @@ pub fn capacity(&self) -> uint {
     /// assert!(map.capacity() >= 10);
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
-    pub fn reserve_len(&mut self, len: uint) {
+    pub fn reserve_len(&mut self, len: usize) {
         let cur_len = self.v.len();
         if len >= cur_len {
             self.v.reserve(len - cur_len);
@@ -179,7 +205,7 @@ pub fn reserve_len(&mut self, len: uint) {
     /// assert!(map.capacity() >= 10);
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
-    pub fn reserve_len_exact(&mut self, len: uint) {
+    pub fn reserve_len_exact(&mut self, len: usize) {
         let cur_len = self.v.len();
         if len >= cur_len {
             self.v.reserve_exact(len - cur_len);
@@ -187,11 +213,11 @@ pub fn reserve_len_exact(&mut self, len: uint) {
     }
 
     /// Returns an iterator visiting all keys in ascending order of the keys.
-    /// The iterator's element type is `uint`.
+    /// The iterator's element type is `usize`.
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn keys<'r>(&'r self) -> Keys<'r, V> {
         fn first<A, B>((a, _): (A, B)) -> A { a }
-        let first: fn((uint, &'r V)) -> uint = first; // coerce to fn pointer
+        let first: fn((usize, &'r V)) -> usize = first; // coerce to fn pointer
 
         Keys { iter: self.iter().map(first) }
     }
@@ -201,13 +227,13 @@ fn first<A, B>((a, _): (A, B)) -> A { a }
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn values<'r>(&'r self) -> Values<'r, V> {
         fn second<A, B>((_, b): (A, B)) -> B { b }
-        let second: fn((uint, &'r V)) -> &'r V = second; // coerce to fn pointer
+        let second: fn((usize, &'r V)) -> &'r V = second; // coerce to fn pointer
 
         Values { iter: self.iter().map(second) }
     }
 
     /// Returns an iterator visiting all key-value pairs in ascending order of the keys.
-    /// The iterator's element type is `(uint, &'r V)`.
+    /// The iterator's element type is `(usize, &'r V)`.
     ///
     /// # Examples
     ///
@@ -235,7 +261,7 @@ pub fn iter<'r>(&'r self) -> Iter<'r, V> {
 
     /// Returns an iterator visiting all key-value pairs in ascending order of the keys,
     /// with mutable references to the values.
-    /// The iterator's element type is `(uint, &'r mut V)`.
+    /// The iterator's element type is `(usize, &'r mut V)`.
     ///
     /// # Examples
     ///
@@ -266,7 +292,7 @@ pub fn iter_mut<'r>(&'r mut self) -> IterMut<'r, V> {
 
     /// Returns an iterator visiting all key-value pairs in ascending order of
     /// the keys, consuming the original `VecMap`.
-    /// The iterator's element type is `(uint, &'r V)`.
+    /// The iterator's element type is `(usize, &'r V)`.
     ///
     /// # Examples
     ///
@@ -278,23 +304,23 @@ pub fn iter_mut<'r>(&'r mut self) -> IterMut<'r, V> {
     /// map.insert(3, "c");
     /// map.insert(2, "b");
     ///
-    /// let vec: Vec<(uint, &str)> = map.into_iter().collect();
+    /// let vec: Vec<(usize, &str)> = map.into_iter().collect();
     ///
     /// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn into_iter(self) -> IntoIter<V> {
-        fn filter<A>((i, v): (uint, Option<A>)) -> Option<(uint, A)> {
+        fn filter<A>((i, v): (usize, Option<A>)) -> Option<(usize, A)> {
             v.map(|v| (i, v))
         }
-        let filter: fn((uint, Option<V>)) -> Option<(uint, V)> = filter; // coerce to fn ptr
+        let filter: fn((usize, Option<V>)) -> Option<(usize, V)> = filter; // coerce to fn ptr
 
         IntoIter { iter: self.v.into_iter().enumerate().filter_map(filter) }
     }
 
     /// Returns an iterator visiting all key-value pairs in ascending order of
     /// the keys, emptying (but not consuming) the original `VecMap`.
-    /// The iterator's element type is `(uint, &'r V)`. Keeps the allocated memory for reuse.
+    /// The iterator's element type is `(usize, &'r V)`. Keeps the allocated memory for reuse.
     ///
     /// # Examples
     ///
@@ -306,17 +332,17 @@ fn filter<A>((i, v): (uint, Option<A>)) -> Option<(uint, A)> {
     /// map.insert(3, "c");
     /// map.insert(2, "b");
     ///
-    /// let vec: Vec<(uint, &str)> = map.drain().collect();
+    /// let vec: Vec<(usize, &str)> = map.drain().collect();
     ///
     /// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
     /// ```
     #[unstable(feature = "collections",
                reason = "matches collection reform specification, waiting for dust to settle")]
     pub fn drain<'a>(&'a mut self) -> Drain<'a, V> {
-        fn filter<A>((i, v): (uint, Option<A>)) -> Option<(uint, A)> {
+        fn filter<A>((i, v): (usize, Option<A>)) -> Option<(usize, A)> {
             v.map(|v| (i, v))
         }
-        let filter: fn((uint, Option<V>)) -> Option<(uint, V)> = filter; // coerce to fn ptr
+        let filter: fn((usize, Option<V>)) -> Option<(usize, V)> = filter; // coerce to fn ptr
 
         Drain { iter: self.v.drain().enumerate().filter_map(filter) }
     }
@@ -334,7 +360,7 @@ fn filter<A>((i, v): (uint, Option<A>)) -> Option<(uint, A)> {
     /// assert_eq!(a.len(), 1);
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
-    pub fn len(&self) -> uint {
+    pub fn len(&self) -> usize {
         self.v.iter().filter(|elt| elt.is_some()).count()
     }
 
@@ -383,7 +409,7 @@ pub fn clear(&mut self) { self.v.clear() }
     /// assert_eq!(map.get(&2), None);
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
-    pub fn get(&self, key: &uint) -> Option<&V> {
+    pub fn get(&self, key: &usize) -> Option<&V> {
         if *key < self.v.len() {
             match self.v[*key] {
               Some(ref value) => Some(value),
@@ -408,7 +434,7 @@ pub fn get(&self, key: &uint) -> Option<&V> {
     /// ```
     #[inline]
     #[stable(feature = "rust1", since = "1.0.0")]
-    pub fn contains_key(&self, key: &uint) -> bool {
+    pub fn contains_key(&self, key: &usize) -> bool {
         self.get(key).is_some()
     }
 
@@ -428,7 +454,7 @@ pub fn contains_key(&self, key: &uint) -> bool {
     /// assert_eq!(map[1], "b");
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
-    pub fn get_mut(&mut self, key: &uint) -> Option<&mut V> {
+    pub fn get_mut(&mut self, key: &usize) -> Option<&mut V> {
         if *key < self.v.len() {
             match *(&mut self.v[*key]) {
               Some(ref mut value) => Some(value),
@@ -456,7 +482,7 @@ pub fn get_mut(&mut self, key: &uint) -> Option<&mut V> {
     /// assert_eq!(map[37], "c");
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
-    pub fn insert(&mut self, key: uint, value: V) -> Option<V> {
+    pub fn insert(&mut self, key: usize, value: V) -> Option<V> {
         let len = self.v.len();
         if len <= key {
             self.v.extend((0..key - len + 1).map(|_| None));
@@ -478,13 +504,119 @@ pub fn insert(&mut self, key: uint, value: V) -> Option<V> {
     /// assert_eq!(map.remove(&1), None);
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
-    pub fn remove(&mut self, key: &uint) -> Option<V> {
+    pub fn remove(&mut self, key: &usize) -> Option<V> {
         if *key >= self.v.len() {
             return None;
         }
         let result = &mut self.v[*key];
         result.take()
     }
+
+    /// Gets the given key's corresponding entry in the map for in-place manipulation.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::collections::VecMap;
+    /// use std::collections::vec_map::Entry;
+    ///
+    /// let mut count: VecMap<u32> = VecMap::new();
+    ///
+    /// // count the number of occurrences of numbers in the vec
+    /// for x in vec![1, 2, 1, 2, 3, 4, 1, 2, 4].iter() {
+    ///     match count.entry(*x) {
+    ///         Entry::Vacant(view) => {
+    ///             view.insert(1);
+    ///         },
+    ///         Entry::Occupied(mut view) => {
+    ///             let v = view.get_mut();
+    ///             *v += 1;
+    ///         },
+    ///     }
+    /// }
+    ///
+    /// assert_eq!(count[1], 3);
+    /// ```
+    #[stable(feature = "rust1", since = "1.0.0")]
+    pub fn entry(&mut self, key: usize) -> Entry<V> {
+        // FIXME(Gankro): this is basically the dumbest implementation of
+        // entry possible, because weird non-lexical borrows issues make it
+        // completely insane to do any other way. That said, Entry is a border-line
+        // useless construct on VecMap, so it's hardly a big loss.
+        if self.contains_key(&key) {
+            Occupied(OccupiedEntry {
+                map: self,
+                index: key,
+            })
+        } else {
+            Vacant(VacantEntry {
+                map: self,
+                index: key,
+            })
+        }
+    }
+}
+
+
+impl<'a, V> Entry<'a, V> {
+    #[unstable(feature = "collections",
+               reason = "matches collection reform v2 specification, waiting for dust to settle")]
+    /// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant
+    pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, V>> {
+        match self {
+            Occupied(entry) => Ok(entry.into_mut()),
+            Vacant(entry) => Err(entry),
+        }
+    }
+}
+
+impl<'a, V> VacantEntry<'a, V> {
+    /// Sets the value of the entry with the VacantEntry's key,
+    /// and returns a mutable reference to it.
+    #[stable(feature = "rust1", since = "1.0.0")]
+    pub fn insert(self, value: V) -> &'a mut V {
+        let index = self.index;
+        self.map.insert(index, value);
+        &mut self.map[index]
+    }
+}
+
+impl<'a, V> OccupiedEntry<'a, V> {
+    /// Gets a reference to the value in the entry.
+    #[stable(feature = "rust1", since = "1.0.0")]
+    pub fn get(&self) -> &V {
+        let index = self.index;
+        &self.map[index]
+    }
+
+    /// Gets a mutable reference to the value in the entry.
+    #[stable(feature = "rust1", since = "1.0.0")]
+    pub fn get_mut(&mut self) -> &mut V {
+        let index = self.index;
+        &mut self.map[index]
+    }
+
+    /// Converts the entry into a mutable reference to its value.
+    #[stable(feature = "rust1", since = "1.0.0")]
+    pub fn into_mut(self) -> &'a mut V {
+        let index = self.index;
+        &mut self.map[index]
+    }
+
+    /// Sets the value of the entry with the OccupiedEntry's key,
+    /// and returns the entry's old value.
+    #[stable(feature = "rust1", since = "1.0.0")]
+    pub fn insert(&mut self, value: V) -> V {
+        let index = self.index;
+        self.map.insert(index, value).unwrap()
+    }
+
+    /// Takes the value of the entry out of the map, and returns it.
+    #[stable(feature = "rust1", since = "1.0.0")]
+    pub fn remove(self) -> V {
+        let index = self.index;
+        self.map.remove(&index).unwrap()
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
@@ -528,8 +660,8 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl<V> FromIterator<(uint, V)> for VecMap<V> {
-    fn from_iter<Iter: Iterator<Item=(uint, V)>>(iter: Iter) -> VecMap<V> {
+impl<V> FromIterator<(usize, V)> for VecMap<V> {
+    fn from_iter<Iter: Iterator<Item=(usize, V)>>(iter: Iter) -> VecMap<V> {
         let mut map = VecMap::new();
         map.extend(iter);
         map
@@ -561,29 +693,27 @@ fn into_iter(mut self) -> IterMut<'a, T> {
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl<V> Extend<(uint, V)> for VecMap<V> {
-    fn extend<Iter: Iterator<Item=(uint, V)>>(&mut self, mut iter: Iter) {
+impl<V> Extend<(usize, V)> for VecMap<V> {
+    fn extend<Iter: Iterator<Item=(usize, V)>>(&mut self, iter: Iter) {
         for (k, v) in iter {
             self.insert(k, v);
         }
     }
 }
 
-impl<V> Index<uint> for VecMap<V> {
+impl<V> Index<usize> for VecMap<V> {
     type Output = V;
 
     #[inline]
-    fn index<'a>(&'a self, i: &uint) -> &'a V {
+    fn index<'a>(&'a self, i: &usize) -> &'a V {
         self.get(i).expect("key not present")
     }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl<V> IndexMut<uint> for VecMap<V> {
-    type Output = V;
-
+impl<V> IndexMut<usize> for VecMap<V> {
     #[inline]
-    fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut V {
+    fn index_mut<'a>(&'a mut self, i: &usize) -> &'a mut V {
         self.get_mut(i).expect("key not present")
     }
 }
@@ -616,7 +746,7 @@ fn next(&mut self) -> Option<$elem> {
             }
 
             #[inline]
-            fn size_hint(&self) -> (uint, Option<uint>) {
+            fn size_hint(&self) -> (usize, Option<usize>) {
                 (0, Some(self.back - self.front))
             }
         }
@@ -653,8 +783,8 @@ fn next_back(&mut self) -> Option<$elem> {
 /// An iterator over the key-value pairs of a map.
 #[stable(feature = "rust1", since = "1.0.0")]
 pub struct Iter<'a, V:'a> {
-    front: uint,
-    back: uint,
+    front: usize,
+    back: usize,
     iter: slice::Iter<'a, Option<V>>
 }
 
@@ -669,25 +799,25 @@ fn clone(&self) -> Iter<'a, V> {
     }
 }
 
-iterator! { impl Iter -> (uint, &'a V), as_ref }
-double_ended_iterator! { impl Iter -> (uint, &'a V), as_ref }
+iterator! { impl Iter -> (usize, &'a V), as_ref }
+double_ended_iterator! { impl Iter -> (usize, &'a V), as_ref }
 
 /// An iterator over the key-value pairs of a map, with the
 /// values being mutable.
 #[stable(feature = "rust1", since = "1.0.0")]
 pub struct IterMut<'a, V:'a> {
-    front: uint,
-    back: uint,
+    front: usize,
+    back: usize,
     iter: slice::IterMut<'a, Option<V>>
 }
 
-iterator! { impl IterMut -> (uint, &'a mut V), as_mut }
-double_ended_iterator! { impl IterMut -> (uint, &'a mut V), as_mut }
+iterator! { impl IterMut -> (usize, &'a mut V), as_mut }
+double_ended_iterator! { impl IterMut -> (usize, &'a mut V), as_mut }
 
 /// An iterator over the keys of a map.
 #[stable(feature = "rust1", since = "1.0.0")]
 pub struct Keys<'a, V: 'a> {
-    iter: Map<(uint, &'a V), uint, Iter<'a, V>, fn((uint, &'a V)) -> uint>
+    iter: Map<Iter<'a, V>, fn((usize, &'a V)) -> usize>
 }
 
 // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
@@ -702,7 +832,7 @@ fn clone(&self) -> Keys<'a, V> {
 /// An iterator over the values of a map.
 #[stable(feature = "rust1", since = "1.0.0")]
 pub struct Values<'a, V: 'a> {
-    iter: Map<(uint, &'a V), &'a V, Iter<'a, V>, fn((uint, &'a V)) -> &'a V>
+    iter: Map<Iter<'a, V>, fn((usize, &'a V)) -> &'a V>
 }
 
 // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
@@ -718,44 +848,40 @@ fn clone(&self) -> Values<'a, V> {
 #[stable(feature = "rust1", since = "1.0.0")]
 pub struct IntoIter<V> {
     iter: FilterMap<
-    (uint, Option<V>),
-    (uint, V),
     Enumerate<vec::IntoIter<Option<V>>>,
-    fn((uint, Option<V>)) -> Option<(uint, V)>>
+    fn((usize, Option<V>)) -> Option<(usize, V)>>
 }
 
 #[unstable(feature = "collections")]
 pub struct Drain<'a, V> {
     iter: FilterMap<
-    (uint, Option<V>),
-    (uint, V),
     Enumerate<vec::Drain<'a, Option<V>>>,
-    fn((uint, Option<V>)) -> Option<(uint, V)>>
+    fn((usize, Option<V>)) -> Option<(usize, V)>>
 }
 
 #[unstable(feature = "collections")]
 impl<'a, V> Iterator for Drain<'a, V> {
-    type Item = (uint, V);
+    type Item = (usize, V);
 
-    fn next(&mut self) -> Option<(uint, V)> { self.iter.next() }
-    fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
+    fn next(&mut self) -> Option<(usize, V)> { self.iter.next() }
+    fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
 }
 
 #[unstable(feature = "collections")]
 impl<'a, V> DoubleEndedIterator for Drain<'a, V> {
-    fn next_back(&mut self) -> Option<(uint, V)> { self.iter.next_back() }
+    fn next_back(&mut self) -> Option<(usize, V)> { self.iter.next_back() }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<'a, V> Iterator for Keys<'a, V> {
-    type Item = uint;
+    type Item = usize;
 
-    fn next(&mut self) -> Option<uint> { self.iter.next() }
-    fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
+    fn next(&mut self) -> Option<usize> { self.iter.next() }
+    fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
 }
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<'a, V> DoubleEndedIterator for Keys<'a, V> {
-    fn next_back(&mut self) -> Option<uint> { self.iter.next_back() }
+    fn next_back(&mut self) -> Option<usize> { self.iter.next_back() }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]
@@ -763,7 +889,7 @@ impl<'a, V> Iterator for Values<'a, V> {
     type Item = &'a V;
 
     fn next(&mut self) -> Option<(&'a V)> { self.iter.next() }
-    fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
+    fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
 }
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<'a, V> DoubleEndedIterator for Values<'a, V> {
@@ -772,14 +898,14 @@ fn next_back(&mut self) -> Option<(&'a V)> { self.iter.next_back() }
 
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<V> Iterator for IntoIter<V> {
-    type Item = (uint, V);
+    type Item = (usize, V);
 
-    fn next(&mut self) -> Option<(uint, V)> { self.iter.next() }
-    fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
+    fn next(&mut self) -> Option<(usize, V)> { self.iter.next() }
+    fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
 }
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<V> DoubleEndedIterator for IntoIter<V> {
-    fn next_back(&mut self) -> Option<(uint, V)> { self.iter.next_back() }
+    fn next_back(&mut self) -> Option<(usize, V)> { self.iter.next_back() }
 }
 
 #[cfg(test)]
@@ -788,11 +914,12 @@ mod test_map {
     use core::hash::{hash, SipHasher};
 
     use super::VecMap;
+    use super::Entry::{Occupied, Vacant};
 
     #[test]
     fn test_get_mut() {
         let mut m = VecMap::new();
-        assert!(m.insert(1, 12i).is_none());
+        assert!(m.insert(1, 12).is_none());
         assert!(m.insert(2, 8).is_none());
         assert!(m.insert(5, 14).is_none());
         let new = 100;
@@ -807,7 +934,7 @@ fn test_len() {
         let mut map = VecMap::new();
         assert_eq!(map.len(), 0);
         assert!(map.is_empty());
-        assert!(map.insert(5, 20i).is_none());
+        assert!(map.insert(5, 20).is_none());
         assert_eq!(map.len(), 1);
         assert!(!map.is_empty());
         assert!(map.insert(11, 12).is_none());
@@ -821,7 +948,7 @@ fn test_len() {
     #[test]
     fn test_clear() {
         let mut map = VecMap::new();
-        assert!(map.insert(5, 20i).is_none());
+        assert!(map.insert(5, 20).is_none());
         assert!(map.insert(11, 12).is_none());
         assert!(map.insert(14, 22).is_none());
         map.clear();
@@ -834,15 +961,15 @@ fn test_clear() {
     #[test]
     fn test_insert() {
         let mut m = VecMap::new();
-        assert_eq!(m.insert(1, 2i), None);
-        assert_eq!(m.insert(1, 3i), Some(2));
-        assert_eq!(m.insert(1, 4i), Some(3));
+        assert_eq!(m.insert(1, 2), None);
+        assert_eq!(m.insert(1, 3), Some(2));
+        assert_eq!(m.insert(1, 4), Some(3));
     }
 
     #[test]
     fn test_remove() {
         let mut m = VecMap::new();
-        m.insert(1, 2i);
+        m.insert(1, 2);
         assert_eq!(m.remove(&1), Some(2));
         assert_eq!(m.remove(&1), None);
     }
@@ -853,7 +980,7 @@ fn test_keys() {
         map.insert(1, 'a');
         map.insert(2, 'b');
         map.insert(3, 'c');
-        let keys = map.keys().collect::<Vec<uint>>();
+        let keys: Vec<_> = map.keys().collect();
         assert_eq!(keys.len(), 3);
         assert!(keys.contains(&1));
         assert!(keys.contains(&2));
@@ -866,7 +993,7 @@ fn test_values() {
         map.insert(1, 'a');
         map.insert(2, 'b');
         map.insert(3, 'c');
-        let values = map.values().map(|&v| v).collect::<Vec<char>>();
+        let values: Vec<_> = map.values().cloned().collect();
         assert_eq!(values.len(), 3);
         assert!(values.contains(&'a'));
         assert!(values.contains(&'b'));
@@ -877,7 +1004,7 @@ fn test_values() {
     fn test_iterator() {
         let mut m = VecMap::new();
 
-        assert!(m.insert(0, 1i).is_none());
+        assert!(m.insert(0, 1).is_none());
         assert!(m.insert(1, 2).is_none());
         assert!(m.insert(3, 5).is_none());
         assert!(m.insert(6, 10).is_none());
@@ -902,7 +1029,7 @@ fn test_iterator() {
     fn test_iterator_size_hints() {
         let mut m = VecMap::new();
 
-        assert!(m.insert(0, 1i).is_none());
+        assert!(m.insert(0, 1).is_none());
         assert!(m.insert(1, 2).is_none());
         assert!(m.insert(3, 5).is_none());
         assert!(m.insert(6, 10).is_none());
@@ -918,14 +1045,14 @@ fn test_iterator_size_hints() {
     fn test_mut_iterator() {
         let mut m = VecMap::new();
 
-        assert!(m.insert(0, 1i).is_none());
+        assert!(m.insert(0, 1).is_none());
         assert!(m.insert(1, 2).is_none());
         assert!(m.insert(3, 5).is_none());
         assert!(m.insert(6, 10).is_none());
         assert!(m.insert(10, 11).is_none());
 
-        for (k, v) in m.iter_mut() {
-            *v += k as int;
+        for (k, v) in &mut m {
+            *v += k as isize;
         }
 
         let mut it = m.iter();
@@ -941,7 +1068,7 @@ fn test_mut_iterator() {
     fn test_rev_iterator() {
         let mut m = VecMap::new();
 
-        assert!(m.insert(0, 1i).is_none());
+        assert!(m.insert(0, 1).is_none());
         assert!(m.insert(1, 2).is_none());
         assert!(m.insert(3, 5).is_none());
         assert!(m.insert(6, 10).is_none());
@@ -960,14 +1087,14 @@ fn test_rev_iterator() {
     fn test_mut_rev_iterator() {
         let mut m = VecMap::new();
 
-        assert!(m.insert(0, 1i).is_none());
+        assert!(m.insert(0, 1).is_none());
         assert!(m.insert(1, 2).is_none());
         assert!(m.insert(3, 5).is_none());
         assert!(m.insert(6, 10).is_none());
         assert!(m.insert(10, 11).is_none());
 
         for (k, v) in m.iter_mut().rev() {
-            *v += k as int;
+            *v += k as isize;
         }
 
         let mut it = m.iter();
@@ -982,13 +1109,13 @@ fn test_mut_rev_iterator() {
     #[test]
     fn test_move_iter() {
         let mut m = VecMap::new();
-        m.insert(1, box 2i);
+        m.insert(1, box 2);
         let mut called = false;
-        for (k, v) in m.into_iter() {
+        for (k, v) in m {
             assert!(!called);
             called = true;
             assert_eq!(k, 1);
-            assert_eq!(v, box 2i);
+            assert_eq!(v, box 2);
         }
         assert!(called);
     }
@@ -1000,7 +1127,7 @@ fn test_drain_iterator() {
         map.insert(3, "c");
         map.insert(2, "b");
 
-        let vec: Vec<(usize, &str)> = map.drain().collect();
+        let vec: Vec<_> = map.drain().collect();
 
         assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
         assert_eq!(map.len(), 0);
@@ -1009,10 +1136,10 @@ fn test_drain_iterator() {
     #[test]
     fn test_show() {
         let mut map = VecMap::new();
-        let empty = VecMap::<int>::new();
+        let empty = VecMap::<i32>::new();
 
-        map.insert(1, 2i);
-        map.insert(3, 4i);
+        map.insert(1, 2);
+        map.insert(3, 4);
 
         let map_str = format!("{:?}", map);
         assert!(map_str == "VecMap {1: 2, 3: 4}" || map_str == "{3: 4, 1: 2}");
@@ -1036,9 +1163,9 @@ fn test_eq() {
         let mut b = VecMap::new();
 
         assert!(a == b);
-        assert!(a.insert(0, 5i).is_none());
+        assert!(a.insert(0, 5).is_none());
         assert!(a != b);
-        assert!(b.insert(0, 4i).is_none());
+        assert!(b.insert(0, 4).is_none());
         assert!(a != b);
         assert!(a.insert(5, 19).is_none());
         assert!(a != b);
@@ -1058,7 +1185,7 @@ fn test_lt() {
         let mut b = VecMap::new();
 
         assert!(!(a < b) && !(b < a));
-        assert!(b.insert(2u, 5i).is_none());
+        assert!(b.insert(2, 5).is_none());
         assert!(a < b);
         assert!(a.insert(2, 7).is_none());
         assert!(!(a < b) && b < a);
@@ -1076,7 +1203,7 @@ fn test_ord() {
         let mut b = VecMap::new();
 
         assert!(a <= b && a >= b);
-        assert!(a.insert(1u, 1i).is_none());
+        assert!(a.insert(1, 1).is_none());
         assert!(a > b && a >= b);
         assert!(b < a && b <= a);
         assert!(b.insert(2, 2).is_none());
@@ -1108,18 +1235,18 @@ fn test_hash() {
 
     #[test]
     fn test_from_iter() {
-        let xs: Vec<(uint, char)> = vec![(1u, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')];
+        let xs = vec![(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')];
 
-        let map: VecMap<char> = xs.iter().map(|&x| x).collect();
+        let map: VecMap<_> = xs.iter().cloned().collect();
 
-        for &(k, v) in xs.iter() {
+        for &(k, v) in &xs {
             assert_eq!(map.get(&k), Some(&v));
         }
     }
 
     #[test]
     fn test_index() {
-        let mut map: VecMap<int> = VecMap::new();
+        let mut map = VecMap::new();
 
         map.insert(1, 2);
         map.insert(2, 1);
@@ -1131,7 +1258,7 @@ fn test_index() {
     #[test]
     #[should_fail]
     fn test_index_nonexistent() {
-        let mut map: VecMap<int> = VecMap::new();
+        let mut map = VecMap::new();
 
         map.insert(1, 2);
         map.insert(2, 1);
@@ -1139,6 +1266,57 @@ fn test_index_nonexistent() {
 
         map[4];
     }
+
+    #[test]
+    fn test_entry(){
+        let xs = [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)];
+
+        let mut map: VecMap<_> = xs.iter().cloned().collect();
+
+        // Existing key (insert)
+        match map.entry(1) {
+            Vacant(_) => unreachable!(),
+            Occupied(mut view) => {
+                assert_eq!(view.get(), &10);
+                assert_eq!(view.insert(100), 10);
+            }
+        }
+        assert_eq!(map.get(&1).unwrap(), &100);
+        assert_eq!(map.len(), 6);
+
+
+        // Existing key (update)
+        match map.entry(2) {
+            Vacant(_) => unreachable!(),
+            Occupied(mut view) => {
+                let v = view.get_mut();
+                *v *= 10;
+            }
+        }
+        assert_eq!(map.get(&2).unwrap(), &200);
+        assert_eq!(map.len(), 6);
+
+        // Existing key (take)
+        match map.entry(3) {
+            Vacant(_) => unreachable!(),
+            Occupied(view) => {
+                assert_eq!(view.remove(), 30);
+            }
+        }
+        assert_eq!(map.get(&3), None);
+        assert_eq!(map.len(), 5);
+
+
+        // Inexistent key (insert)
+        match map.entry(10) {
+            Occupied(_) => unreachable!(),
+            Vacant(view) => {
+                assert_eq!(*view.insert(1000), 1000);
+            }
+        }
+        assert_eq!(map.get(&10).unwrap(), &1000);
+        assert_eq!(map.len(), 6);
+    }
 }
 
 #[cfg(test)]
@@ -1149,7 +1327,7 @@ mod bench {
 
     #[bench]
     pub fn insert_rand_100(b: &mut Bencher) {
-        let mut m : VecMap<uint> = VecMap::new();
+        let mut m = VecMap::new();
         insert_rand_n(100, &mut m, b,
                       |m, i| { m.insert(i, 1); },
                       |m, i| { m.remove(&i); });
@@ -1157,7 +1335,7 @@ pub fn insert_rand_100(b: &mut Bencher) {
 
     #[bench]
     pub fn insert_rand_10_000(b: &mut Bencher) {
-        let mut m : VecMap<uint> = VecMap::new();
+        let mut m = VecMap::new();
         insert_rand_n(10_000, &mut m, b,
                       |m, i| { m.insert(i, 1); },
                       |m, i| { m.remove(&i); });
@@ -1166,7 +1344,7 @@ pub fn insert_rand_10_000(b: &mut Bencher) {
     // Insert seq
     #[bench]
     pub fn insert_seq_100(b: &mut Bencher) {
-        let mut m : VecMap<uint> = VecMap::new();
+        let mut m = VecMap::new();
         insert_seq_n(100, &mut m, b,
                      |m, i| { m.insert(i, 1); },
                      |m, i| { m.remove(&i); });
@@ -1174,7 +1352,7 @@ pub fn insert_seq_100(b: &mut Bencher) {
 
     #[bench]
     pub fn insert_seq_10_000(b: &mut Bencher) {
-        let mut m : VecMap<uint> = VecMap::new();
+        let mut m = VecMap::new();
         insert_seq_n(10_000, &mut m, b,
                      |m, i| { m.insert(i, 1); },
                      |m, i| { m.remove(&i); });
@@ -1183,7 +1361,7 @@ pub fn insert_seq_10_000(b: &mut Bencher) {
     // Find rand
     #[bench]
     pub fn find_rand_100(b: &mut Bencher) {
-        let mut m : VecMap<uint> = VecMap::new();
+        let mut m = VecMap::new();
         find_rand_n(100, &mut m, b,
                     |m, i| { m.insert(i, 1); },
                     |m, i| { m.get(&i); });
@@ -1191,7 +1369,7 @@ pub fn find_rand_100(b: &mut Bencher) {
 
     #[bench]
     pub fn find_rand_10_000(b: &mut Bencher) {
-        let mut m : VecMap<uint> = VecMap::new();
+        let mut m = VecMap::new();
         find_rand_n(10_000, &mut m, b,
                     |m, i| { m.insert(i, 1); },
                     |m, i| { m.get(&i); });
@@ -1200,7 +1378,7 @@ pub fn find_rand_10_000(b: &mut Bencher) {
     // Find seq
     #[bench]
     pub fn find_seq_100(b: &mut Bencher) {
-        let mut m : VecMap<uint> = VecMap::new();
+        let mut m = VecMap::new();
         find_seq_n(100, &mut m, b,
                    |m, i| { m.insert(i, 1); },
                    |m, i| { m.get(&i); });
@@ -1208,7 +1386,7 @@ pub fn find_seq_100(b: &mut Bencher) {
 
     #[bench]
     pub fn find_seq_10_000(b: &mut Bencher) {
-        let mut m : VecMap<uint> = VecMap::new();
+        let mut m = VecMap::new();
         find_seq_n(10_000, &mut m, b,
                    |m, i| { m.insert(i, 1); },
                    |m, i| { m.get(&i); });