diff options
| author | Jorge Aparicio <japaricious@gmail.com> | 2015-01-07 22:01:05 -0500 |
|---|---|---|
| committer | Jorge Aparicio <japaricious@gmail.com> | 2015-01-30 10:36:31 -0500 |
| commit | a65d3f5b98cc94f0a759fbf1a08be9aee0f97883 (patch) | |
| tree | 727f2112924bbe042cb6a1ea059d163044ace818 /src/libcore | |
| parent | 1a51eb9cca3ae5f815825096de4dfbdc9267f735 (diff) | |
| download | rust-a65d3f5b98cc94f0a759fbf1a08be9aee0f97883.tar.gz rust-a65d3f5b98cc94f0a759fbf1a08be9aee0f97883.zip | |
core: add the `IntoIterator` trait
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/array.rs | 18 | ||||
| -rw-r--r-- | src/libcore/iter.rs | 17 | ||||
| -rw-r--r-- | src/libcore/slice.rs | 16 |
3 files changed, 51 insertions, 0 deletions
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) => { |
