about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJorge Aparicio <japaricious@gmail.com>2015-01-07 22:01:05 -0500
committerJorge Aparicio <japaricious@gmail.com>2015-01-30 10:36:31 -0500
commita65d3f5b98cc94f0a759fbf1a08be9aee0f97883 (patch)
tree727f2112924bbe042cb6a1ea059d163044ace818
parent1a51eb9cca3ae5f815825096de4dfbdc9267f735 (diff)
core: add the `IntoIterator` trait
-rw-r--r--src/libcollections/binary_heap.rs18
-rw-r--r--src/libcollections/bit.rs17
-rw-r--r--src/libcollections/btree/map.rs26
-rw-r--r--src/libcollections/btree/set.rs18
-rw-r--r--src/libcollections/dlist.rs26
-rw-r--r--src/libcollections/enum_set.rs10
-rw-r--r--src/libcollections/ring_buf.rs26
-rw-r--r--src/libcollections/vec.rs27
-rw-r--r--src/libcollections/vec_map.rs26
-rw-r--r--src/libcore/array.rs18
-rw-r--r--src/libcore/iter.rs17
-rw-r--r--src/libcore/slice.rs16
12 files changed, 236 insertions, 9 deletions
diff --git a/src/libcollections/binary_heap.rs b/src/libcollections/binary_heap.rs
index f717fc6075d..fb4c326e10a 100644
--- a/src/libcollections/binary_heap.rs
+++ b/src/libcollections/binary_heap.rs
@@ -153,7 +153,7 @@
 use core::prelude::*;
 
 use core::default::Default;
-use core::iter::FromIterator;
+use core::iter::{FromIterator, IntoIterator};
 use core::mem::{zeroed, replace, swap};
 use core::ptr;
 
@@ -655,6 +655,22 @@ impl<T: Ord> FromIterator<T> for BinaryHeap<T> {
     }
 }
 
+impl<T> IntoIterator for BinaryHeap<T> {
+    type Iter = IntoIter<T>;
+
+    fn into_iter(self) -> IntoIter<T> {
+        self.into_iter()
+    }
+}
+
+impl<'a, T> IntoIterator for &'a BinaryHeap<T> where T: Ord {
+    type Iter = Iter<'a, T>;
+
+    fn into_iter(self) -> Iter<'a, T> {
+        self.iter()
+    }
+}
+
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T: Ord> Extend<T> for BinaryHeap<T> {
     fn extend<Iter: Iterator<Item=T>>(&mut self, mut iter: Iter) {
diff --git a/src/libcollections/bit.rs b/src/libcollections/bit.rs
index c6275740579..d676cfca929 100644
--- a/src/libcollections/bit.rs
+++ b/src/libcollections/bit.rs
@@ -89,7 +89,7 @@ use core::fmt;
 use core::hash;
 use core::iter::RandomAccessIterator;
 use core::iter::{Chain, Enumerate, Repeat, Skip, Take, repeat, Cloned};
-use core::iter::{self, FromIterator};
+use core::iter::{self, FromIterator, IntoIterator};
 use core::num::Int;
 use core::ops::Index;
 use core::slice;
@@ -1070,6 +1070,14 @@ impl<'a> RandomAccessIterator for Iter<'a> {
     }
 }
 
+impl<'a> IntoIterator for &'a Bitv {
+    type Iter = Iter<'a>;
+
+    fn into_iter(self) -> Iter<'a> {
+        self.iter()
+    }
+}
+
 /// An implementation of a set using a bit vector as an underlying
 /// representation for holding unsigned numerical elements.
 ///
@@ -1873,6 +1881,13 @@ impl<'a> Iterator for SymmetricDifference<'a> {
     #[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.0.size_hint() }
 }
 
+impl<'a> IntoIterator for &'a BitvSet {
+    type Iter = SetIter<'a>;
+
+    fn into_iter(self) -> SetIter<'a> {
+        self.iter()
+    }
+}
 
 #[cfg(test)]
 mod tests {
diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs
index 4f2c2cb6028..27783ff941a 100644
--- a/src/libcollections/btree/map.rs
+++ b/src/libcollections/btree/map.rs
@@ -24,7 +24,7 @@ use core::cmp::Ordering;
 use core::default::Default;
 use core::fmt::Debug;
 use core::hash::{Hash, Hasher};
-use core::iter::{Map, FromIterator};
+use core::iter::{Map, FromIterator, IntoIterator};
 use core::ops::{Index, IndexMut};
 use core::{iter, fmt, mem};
 use Bound::{self, Included, Excluded, Unbounded};
@@ -478,6 +478,30 @@ impl<K: Ord, V> BTreeMap<K, V> {
     }
 }
 
+impl<K, V> IntoIterator for BTreeMap<K, V> {
+    type Iter = IntoIter<K, V>;
+
+    fn into_iter(self) -> IntoIter<K, V> {
+        self.into_iter()
+    }
+}
+
+impl<'a, K, V> IntoIterator for &'a BTreeMap<K, V> {
+    type Iter = Iter<'a, K, V>;
+
+    fn into_iter(self) -> Iter<'a, K, V> {
+        self.iter()
+    }
+}
+
+impl<'a, K, V> IntoIterator for &'a mut BTreeMap<K, V> {
+    type Iter = IterMut<'a, K, V>;
+
+    fn into_iter(mut self) -> IterMut<'a, K, V> {
+        self.iter_mut()
+    }
+}
+
 /// A helper enum useful for deciding whether to continue a loop since we can't
 /// return from a closure
 enum Continuation<A, B> {
diff --git a/src/libcollections/btree/set.rs b/src/libcollections/btree/set.rs
index a090e4f24ce..e6d7d2a37eb 100644
--- a/src/libcollections/btree/set.rs
+++ b/src/libcollections/btree/set.rs
@@ -18,7 +18,7 @@ use core::cmp::Ordering::{self, Less, Greater, Equal};
 use core::default::Default;
 use core::fmt::Debug;
 use core::fmt;
-use core::iter::{Peekable, Map, FromIterator};
+use core::iter::{Peekable, Map, FromIterator, IntoIterator};
 use core::ops::{BitOr, BitAnd, BitXor, Sub};
 
 use btree_map::{BTreeMap, Keys};
@@ -480,6 +480,22 @@ impl<T: Ord> FromIterator<T> for BTreeSet<T> {
     }
 }
 
+impl<T> IntoIterator for BTreeSet<T> {
+    type Iter = IntoIter<T>;
+
+    fn into_iter(self) -> IntoIter<T> {
+        self.into_iter()
+    }
+}
+
+impl<'a, T> IntoIterator for &'a BTreeSet<T> {
+    type Iter = Iter<'a, T>;
+
+    fn into_iter(self) -> Iter<'a, T> {
+        self.iter()
+    }
+}
+
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T: Ord> Extend<T> for BTreeSet<T> {
     #[inline]
diff --git a/src/libcollections/dlist.rs b/src/libcollections/dlist.rs
index aded4b8a7ac..48bf820e6f6 100644
--- a/src/libcollections/dlist.rs
+++ b/src/libcollections/dlist.rs
@@ -28,7 +28,7 @@ use core::cmp::Ordering;
 use core::default::Default;
 use core::fmt;
 use core::hash::{Writer, Hasher, Hash};
-use core::iter::{self, FromIterator};
+use core::iter::{self, FromIterator, IntoIterator};
 use core::mem;
 use core::ptr;
 
@@ -830,6 +830,30 @@ impl<A> FromIterator<A> for DList<A> {
     }
 }
 
+impl<T> IntoIterator for DList<T> {
+    type Iter = IntoIter<T>;
+
+    fn into_iter(self) -> IntoIter<T> {
+        self.into_iter()
+    }
+}
+
+impl<'a, T> IntoIterator for &'a DList<T> {
+    type Iter = Iter<'a, T>;
+
+    fn into_iter(self) -> Iter<'a, T> {
+        self.iter()
+    }
+}
+
+impl<'a, T> IntoIterator for &'a mut DList<T> {
+    type Iter = IterMut<'a, T>;
+
+    fn into_iter(mut self) -> IterMut<'a, T> {
+        self.iter_mut()
+    }
+}
+
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<A> Extend<A> for DList<A> {
     fn extend<T: Iterator<Item=A>>(&mut self, mut iterator: T) {
diff --git a/src/libcollections/enum_set.rs b/src/libcollections/enum_set.rs
index b542259eba0..a9e64a5c282 100644
--- a/src/libcollections/enum_set.rs
+++ b/src/libcollections/enum_set.rs
@@ -16,7 +16,7 @@
 use core::prelude::*;
 use core::fmt;
 use core::num::Int;
-use core::iter::FromIterator;
+use core::iter::{FromIterator, IntoIterator};
 use core::ops::{Sub, BitOr, BitAnd, BitXor};
 
 // FIXME(contentions): implement union family of methods? (general design may be wrong here)
@@ -256,6 +256,14 @@ impl<E:CLike> FromIterator<E> for EnumSet<E> {
     }
 }
 
+impl<'a, E> IntoIterator for &'a EnumSet<E> where E: CLike {
+    type Iter = Iter<E>;
+
+    fn into_iter(self) -> Iter<E> {
+        self.iter()
+    }
+}
+
 impl<E:CLike> Extend<E> for EnumSet<E> {
     fn extend<I: Iterator<Item=E>>(&mut self, mut iterator: I) {
         for element in iterator {
diff --git a/src/libcollections/ring_buf.rs b/src/libcollections/ring_buf.rs
index 34910f59fe0..5b5e3be12e3 100644
--- a/src/libcollections/ring_buf.rs
+++ b/src/libcollections/ring_buf.rs
@@ -19,7 +19,7 @@ use core::prelude::*;
 use core::cmp::Ordering;
 use core::default::Default;
 use core::fmt;
-use core::iter::{self, repeat, FromIterator, RandomAccessIterator};
+use core::iter::{self, repeat, FromIterator, IntoIterator, RandomAccessIterator};
 use core::marker;
 use core::mem;
 use core::num::{Int, UnsignedInt};
@@ -1609,6 +1609,30 @@ impl<A> FromIterator<A> for RingBuf<A> {
     }
 }
 
+impl<T> IntoIterator for RingBuf<T> {
+    type Iter = IntoIter<T>;
+
+    fn into_iter(self) -> IntoIter<T> {
+        self.into_iter()
+    }
+}
+
+impl<'a, T> IntoIterator for &'a RingBuf<T> {
+    type Iter = Iter<'a, T>;
+
+    fn into_iter(self) -> Iter<'a, T> {
+        self.iter()
+    }
+}
+
+impl<'a, T> IntoIterator for &'a mut RingBuf<T> {
+    type Iter = IterMut<'a, T>;
+
+    fn into_iter(mut self) -> IterMut<'a, T> {
+        self.iter_mut()
+    }
+}
+
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<A> Extend<A> for RingBuf<A> {
     fn extend<T: Iterator<Item=A>>(&mut self, mut iterator: T) {
diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs
index 5dd88dbb025..ac6d7936f28 100644
--- a/src/libcollections/vec.rs
+++ b/src/libcollections/vec.rs
@@ -56,7 +56,7 @@ use core::cmp::{Ordering};
 use core::default::Default;
 use core::fmt;
 use core::hash::{self, Hash};
-use core::iter::{repeat, FromIterator};
+use core::iter::{repeat, FromIterator, IntoIterator};
 use core::marker::{ContravariantLifetime, InvariantType};
 use core::mem;
 use core::nonzero::NonZero;
@@ -65,6 +65,7 @@ use core::ops::{Index, IndexMut, Deref, Add};
 use core::ops;
 use core::ptr;
 use core::raw::Slice as RawSlice;
+use core::slice;
 use core::uint;
 
 /// A growable list type, written `Vec<T>` but pronounced 'vector.'
@@ -1404,6 +1405,30 @@ impl<T> FromIterator<T> for Vec<T> {
     }
 }
 
+impl<T> IntoIterator for Vec<T> {
+    type Iter = IntoIter<T>;
+
+    fn into_iter(self) -> IntoIter<T> {
+        self.into_iter()
+    }
+}
+
+impl<'a, T> IntoIterator for &'a Vec<T> {
+    type Iter = slice::Iter<'a, T>;
+
+    fn into_iter(self) -> slice::Iter<'a, T> {
+        self.iter()
+    }
+}
+
+impl<'a, T> IntoIterator for &'a mut Vec<T> {
+    type Iter = slice::IterMut<'a, T>;
+
+    fn into_iter(mut self) -> slice::IterMut<'a, T> {
+        self.iter_mut()
+    }
+}
+
 #[unstable(feature = "collections", reason = "waiting on Extend stability")]
 impl<T> Extend<T> for Vec<T> {
     #[inline]
diff --git a/src/libcollections/vec_map.rs b/src/libcollections/vec_map.rs
index 9f83b91fc9b..2846414bb9a 100644
--- a/src/libcollections/vec_map.rs
+++ b/src/libcollections/vec_map.rs
@@ -19,7 +19,7 @@ use core::cmp::Ordering;
 use core::default::Default;
 use core::fmt;
 use core::hash::{Hash, Writer, Hasher};
-use core::iter::{Enumerate, FilterMap, Map, FromIterator};
+use core::iter::{Enumerate, FilterMap, Map, FromIterator, IntoIterator};
 use core::iter;
 use core::mem::replace;
 use core::ops::{Index, IndexMut};
@@ -536,6 +536,30 @@ impl<V> FromIterator<(uint, V)> for VecMap<V> {
     }
 }
 
+impl<T> IntoIterator for VecMap<T> {
+    type Iter = IntoIter<T>;
+
+    fn into_iter(self) -> IntoIter<T> {
+        self.into_iter()
+    }
+}
+
+impl<'a, T> IntoIterator for &'a VecMap<T> {
+    type Iter = Iter<'a, T>;
+
+    fn into_iter(self) -> Iter<'a, T> {
+        self.iter()
+    }
+}
+
+impl<'a, T> IntoIterator for &'a mut VecMap<T> {
+    type Iter = IterMut<'a, T>;
+
+    fn into_iter(mut self) -> IterMut<'a, T> {
+        self.iter_mut()
+    }
+}
+
 #[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) {
diff --git a/src/libcore/array.rs b/src/libcore/array.rs
index a81615944fb..ec3d9783255 100644
--- a/src/libcore/array.rs
+++ b/src/libcore/array.rs
@@ -18,12 +18,14 @@ use clone::Clone;
 use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
 use fmt;
 use hash::{Hash, Hasher, self};
+use iter::IntoIterator;
 use marker::Copy;
 #[cfg(stage0)]
 use ops::{Deref, FullRange};
 #[cfg(not(stage0))]
 use ops::Deref;
 use option::Option;
+use slice::{Iter, IterMut, SliceExt};
 
 // macro for implementing n-ary tuple functions and operations
 macro_rules! array_impls {
@@ -49,6 +51,22 @@ macro_rules! array_impls {
                 }
             }
 
+            impl<'a, T> IntoIterator for &'a [T; $N] {
+                type Iter = Iter<'a, T>;
+
+                fn into_iter(self) -> Iter<'a, T> {
+                    self.iter()
+                }
+            }
+
+            impl<'a, T> IntoIterator for &'a mut [T; $N] {
+                type Iter = IterMut<'a, T>;
+
+                fn into_iter(self) -> IterMut<'a, T> {
+                    self.iter_mut()
+                }
+            }
+
             #[stable(feature = "rust1", since = "1.0.0")]
             impl<A, B> PartialEq<[B; $N]> for [A; $N] where A: PartialEq<B> {
                 #[inline]
diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs
index b6b2f9c57fe..f9c6e0758e6 100644
--- a/src/libcore/iter.rs
+++ b/src/libcore/iter.rs
@@ -122,6 +122,23 @@ pub trait FromIterator<A> {
     fn from_iter<T: Iterator<Item=A>>(iterator: T) -> Self;
 }
 
+/// Conversion into an `Iterator`
+#[unstable]
+pub trait IntoIterator {
+    type Iter: Iterator;
+
+    /// Consumes `Self` and returns an iterator over it
+    fn into_iter(self) -> Self::Iter;
+}
+
+impl<I> IntoIterator for I where I: Iterator {
+    type Iter = I;
+
+    fn into_iter(self) -> I {
+        self
+    }
+}
+
 /// A type growable from an `Iterator` implementation
 #[stable(feature = "rust1", since = "1.0.0")]
 pub trait Extend<A> {
diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs
index 40e66db3ae5..11e3d196f73 100644
--- a/src/libcore/slice.rs
+++ b/src/libcore/slice.rs
@@ -637,6 +637,22 @@ impl<'a, T> Default for &'a [T] {
 // Iterators
 //
 
+impl<'a, T> IntoIterator for &'a [T] {
+    type Iter = Iter<'a, T>;
+
+    fn into_iter(self) -> Iter<'a, T> {
+        self.iter()
+    }
+}
+
+impl<'a, T> IntoIterator for &'a mut [T] {
+    type Iter = IterMut<'a, T>;
+
+    fn into_iter(self) -> IterMut<'a, T> {
+        self.iter_mut()
+    }
+}
+
 // The shared definition of the `Iter` and `IterMut` iterators
 macro_rules! iterator {
     (struct $name:ident -> $ptr:ty, $elem:ty) => {