about summary refs log tree commit diff
path: root/src/libcollections
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-01-20 23:03:09 +0000
committerbors <bors@rust-lang.org>2015-01-20 23:03:09 +0000
commit29bd9a06efd2f8c8a7b1102e2203cc0e6ae2dcba (patch)
tree98824cc18b1c65ff09f383438d79ad2d0a0e2ea8 /src/libcollections
parent583c5c589ed02e5b6b14a576e35e0ce68988d949 (diff)
parent631896dc1996d239a532b0ce02d5fe886660149e (diff)
downloadrust-29bd9a06efd2f8c8a7b1102e2203cc0e6ae2dcba.tar.gz
rust-29bd9a06efd2f8c8a7b1102e2203cc0e6ae2dcba.zip
Auto merge of #21439 - alexcrichton:rollup, r=alexcrichton
Continuation of https://github.com/rust-lang/rust/pull/21428
Diffstat (limited to 'src/libcollections')
-rw-r--r--src/libcollections/btree/node.rs6
-rw-r--r--src/libcollections/lib.rs8
-rw-r--r--src/libcollections/ring_buf.rs6
-rw-r--r--src/libcollections/slice.rs2
-rw-r--r--src/libcollections/vec.rs12
-rw-r--r--src/libcollections/vec_map.rs82
6 files changed, 88 insertions, 28 deletions
diff --git a/src/libcollections/btree/node.rs b/src/libcollections/btree/node.rs
index afce5f7dbda..fa890643089 100644
--- a/src/libcollections/btree/node.rs
+++ b/src/libcollections/btree/node.rs
@@ -344,11 +344,11 @@ impl<K, V> Node<K, V> {
     pub fn as_slices<'a>(&'a self) -> (&'a [K], &'a [V]) {
         unsafe {(
             mem::transmute(raw::Slice {
-                data: self.keys.0 as *const K,
+                data: self.keys.0,
                 len: self.len()
             }),
             mem::transmute(raw::Slice {
-                data: self.vals.0 as *const V,
+                data: self.vals.0,
                 len: self.len()
             })
         )}
@@ -368,7 +368,7 @@ impl<K, V> Node<K, V> {
         } else {
             unsafe {
                 mem::transmute(raw::Slice {
-                    data: self.edges.0 as *const Node<K, V>,
+                    data: self.edges.0,
                     len: self.len() + 1
                 })
             }
diff --git a/src/libcollections/lib.rs b/src/libcollections/lib.rs
index 10ea0421eee..6a182add000 100644
--- a/src/libcollections/lib.rs
+++ b/src/libcollections/lib.rs
@@ -102,10 +102,10 @@ pub fn fixme_14344_be_sure_to_link_to_collections() {}
 mod std {
     pub use core::fmt;      // necessary for panic!()
     pub use core::option;   // necessary for panic!()
-    pub use core::clone;    // deriving(Clone)
-    pub use core::cmp;      // deriving(Eq, Ord, etc.)
-    pub use core::marker;  // deriving(Copy)
-    pub use core::hash;     // deriving(Hash)
+    pub use core::clone;    // derive(Clone)
+    pub use core::cmp;      // derive(Eq, Ord, etc.)
+    pub use core::marker;  // derive(Copy)
+    pub use core::hash;     // derive(Hash)
 }
 
 #[cfg(test)]
diff --git a/src/libcollections/ring_buf.rs b/src/libcollections/ring_buf.rs
index c3d22675868..b9cb4be7c18 100644
--- a/src/libcollections/ring_buf.rs
+++ b/src/libcollections/ring_buf.rs
@@ -88,19 +88,19 @@ impl<T> RingBuf<T> {
     /// Turn ptr into a slice
     #[inline]
     unsafe fn buffer_as_slice(&self) -> &[T] {
-        mem::transmute(RawSlice { data: self.ptr as *const T, len: self.cap })
+        mem::transmute(RawSlice { data: self.ptr, len: self.cap })
     }
 
     /// Turn ptr into a mut slice
     #[inline]
     unsafe fn buffer_as_mut_slice(&mut self) -> &mut [T] {
-        mem::transmute(RawSlice { data: self.ptr as *const T, len: self.cap })
+        mem::transmute(RawSlice { data: self.ptr, len: self.cap })
     }
 
     /// Moves an element out of the buffer
     #[inline]
     unsafe fn buffer_read(&mut self, off: uint) -> T {
-        ptr::read(self.ptr.offset(off as int) as *const T)
+        ptr::read(self.ptr.offset(off as int))
     }
 
     /// Writes an element into the buffer, moving it.
diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs
index 4812ecc2c0b..988ec4c661f 100644
--- a/src/libcollections/slice.rs
+++ b/src/libcollections/slice.rs
@@ -1222,7 +1222,7 @@ fn insertion_sort<T, F>(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> O
                                  &*buf_v.offset(j),
                                  (i - j) as uint);
                 ptr::copy_nonoverlapping_memory(buf_v.offset(j),
-                                                &tmp as *const T,
+                                                &tmp,
                                                 1);
                 mem::forget(tmp);
             }
diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs
index 689d96b4b29..4ddab8c533a 100644
--- a/src/libcollections/vec.rs
+++ b/src/libcollections/vec.rs
@@ -426,7 +426,7 @@ impl<T> Vec<T> {
     pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] {
         unsafe {
             mem::transmute(RawSlice {
-                data: *self.ptr as *const T,
+                data: *self.ptr,
                 len: self.len,
             })
         }
@@ -574,7 +574,7 @@ impl<T> Vec<T> {
                 let ptr = self.as_mut_ptr().offset(index as int);
                 // copy it out, unsafely having a copy of the value on
                 // the stack and in the vector at the same time.
-                ret = ptr::read(ptr as *const T);
+                ret = ptr::read(ptr);
 
                 // Shift everything down to fill in that spot.
                 ptr::copy_memory(ptr, &*ptr.offset(1), len - index - 1);
@@ -879,7 +879,7 @@ impl<T> Vec<T> {
                     //          |         |
                     //          end_u     end_t
 
-                    let t = ptr::read(pv.start_t as *const T);
+                    let t = ptr::read(pv.start_t);
                     //  start_u start_t
                     //  |       |
                     // +-+-+-+-+-+-+-+-+-+
@@ -1443,7 +1443,7 @@ impl<T> AsSlice<T> for Vec<T> {
     fn as_slice<'a>(&'a self) -> &'a [T] {
         unsafe {
             mem::transmute(RawSlice {
-                data: *self.ptr as *const T,
+                data: *self.ptr,
                 len: self.len
             })
         }
@@ -1806,11 +1806,11 @@ impl<T,U> Drop for PartialVecNonZeroSized<T,U> {
 
             // We have instances of `U`s and `T`s in `vec`. Destruct them.
             while self.start_u != self.end_u {
-                let _ = ptr::read(self.start_u as *const U); // Run a `U` destructor.
+                let _ = ptr::read(self.start_u); // Run a `U` destructor.
                 self.start_u = self.start_u.offset(1);
             }
             while self.start_t != self.end_t {
-                let _ = ptr::read(self.start_t as *const T); // Run a `T` destructor.
+                let _ = ptr::read(self.start_t); // Run a `T` destructor.
                 self.start_t = self.start_t.offset(1);
             }
             // After this destructor ran, the destructor of `vec` will run,
diff --git a/src/libcollections/vec_map.rs b/src/libcollections/vec_map.rs
index d4ce266d3e2..7ff2e953588 100644
--- a/src/libcollections/vec_map.rs
+++ b/src/libcollections/vec_map.rs
@@ -186,7 +186,7 @@ impl<V> VecMap<V> {
         }
     }
 
-    /// Returns an iterator visiting all keys in ascending order by the keys.
+    /// Returns an iterator visiting all keys in ascending order of the keys.
     /// The iterator's element type is `uint`.
     #[stable]
     pub fn keys<'r>(&'r self) -> Keys<'r, V> {
@@ -196,7 +196,7 @@ impl<V> VecMap<V> {
         Keys { iter: self.iter().map(first) }
     }
 
-    /// Returns an iterator visiting all values in ascending order by the keys.
+    /// Returns an iterator visiting all values in ascending order of the keys.
     /// The iterator's element type is `&'r V`.
     #[stable]
     pub fn values<'r>(&'r self) -> Values<'r, V> {
@@ -206,7 +206,7 @@ impl<V> VecMap<V> {
         Values { iter: self.iter().map(second) }
     }
 
-    /// Returns an iterator visiting all key-value pairs in ascending order by the keys.
+    /// Returns an iterator visiting all key-value pairs in ascending order of the keys.
     /// The iterator's element type is `(uint, &'r V)`.
     ///
     /// # Examples
@@ -233,7 +233,7 @@ impl<V> VecMap<V> {
         }
     }
 
-    /// Returns an iterator visiting all key-value pairs in ascending order by the keys,
+    /// 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)`.
     ///
@@ -264,8 +264,8 @@ impl<V> VecMap<V> {
         }
     }
 
-    /// Returns an iterator visiting all key-value pairs in ascending order by
-    /// the keys, emptying (but not consuming) the original `VecMap`.
+    /// 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)`.
     ///
     /// # Examples
@@ -278,20 +278,46 @@ impl<V> VecMap<V> {
     /// map.insert(3, "c");
     /// map.insert(2, "b");
     ///
-    /// // Not possible with .iter()
     /// let vec: Vec<(uint, &str)> = map.into_iter().collect();
     ///
     /// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
     /// ```
     #[stable]
-    pub fn into_iter(&mut self) -> IntoIter<V> {
+    pub fn into_iter(self) -> IntoIter<V> {
+        fn filter<A>((i, v): (uint, Option<A>)) -> Option<(uint, A)> {
+            v.map(|v| (i, v))
+        }
+        let filter: fn((uint, Option<V>)) -> Option<(uint, 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.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use std::collections::VecMap;
+    ///
+    /// let mut map = VecMap::new();
+    /// map.insert(1, "a");
+    /// map.insert(3, "c");
+    /// map.insert(2, "b");
+    ///
+    /// let vec: Vec<(uint, &str)> = map.drain().collect();
+    ///
+    /// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
+    /// ```
+    #[unstable = "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)> {
             v.map(|v| (i, v))
         }
         let filter: fn((uint, Option<V>)) -> Option<(uint, V)> = filter; // coerce to fn ptr
 
-        let values = replace(&mut self.v, vec!());
-        IntoIter { iter: values.into_iter().enumerate().filter_map(filter) }
+        Drain { iter: self.v.drain().enumerate().filter_map(filter) }
     }
 
     /// Return the number of elements in the map.
@@ -673,6 +699,28 @@ pub struct IntoIter<V> {
     fn((uint, Option<V>)) -> Option<(uint, V)>>
 }
 
+#[unstable]
+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)>>
+}
+
+#[unstable]
+impl<'a, V> Iterator for Drain<'a, V> {
+    type Item = (uint, V);
+
+    fn next(&mut self) -> Option<(uint, V)> { self.iter.next() }
+    fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
+}
+
+#[unstable]
+impl<'a, V> DoubleEndedIterator for Drain<'a, V> {
+    fn next_back(&mut self) -> Option<(uint, V)> { self.iter.next_back() }
+}
+
 #[stable]
 impl<'a, V> Iterator for Keys<'a, V> {
     type Item = uint;
@@ -918,7 +966,19 @@ mod test_map {
             assert_eq!(v, box 2i);
         }
         assert!(called);
-        m.insert(2, box 1i);
+    }
+
+    #[test]
+    fn test_drain_iterator() {
+        let mut map = VecMap::new();
+        map.insert(1, "a");
+        map.insert(3, "c");
+        map.insert(2, "b");
+
+        let vec: Vec<(usize, &str)> = map.drain().collect();
+
+        assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
+        assert_eq!(map.len(), 0);
     }
 
     #[test]