Skip to content

std: Stabilize IteratorExt::cloned #23462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libcollectionstest/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ fn test_mut_rev_iter_wrap() {
assert_eq!(d.pop_front(), Some(1));
d.push_back(4);

assert_eq!(d.iter_mut().rev().cloned().collect::<Vec<_>>(),
assert_eq!(d.iter_mut().rev().map(|x| *x).collect::<Vec<_>>(),
vec![4, 3, 2]);
}

Expand Down
45 changes: 18 additions & 27 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ use default::Default;
use marker;
use mem;
use num::{ToPrimitive, Int};
use ops::{Add, Deref, FnMut, RangeFrom};
use ops::{Add, FnMut, RangeFrom};
use option::Option;
use option::Option::{Some, None};
use marker::Sized;
Expand Down Expand Up @@ -976,12 +976,11 @@ pub trait IteratorExt: Iterator + Sized {
(ts, us)
}

/// Creates an iterator that clones the elements it yields. Useful for converting an
/// Iterator<&T> to an Iterator<T>.
#[unstable(feature = "core", reason = "recent addition")]
fn cloned(self) -> Cloned<Self> where
Self::Item: Deref,
<Self::Item as Deref>::Target: Clone,
/// Creates an iterator that clones the elements it yields. Useful for
/// converting an Iterator<&T> to an Iterator<T>.
#[stable(feature = "rust1", since = "1.0.0")]
fn cloned<'a, T: 'a>(self) -> Cloned<Self>
where Self: Iterator<Item=&'a T>, T: Clone
{
Cloned { it: self }
}
Expand Down Expand Up @@ -1279,14 +1278,12 @@ pub struct Cloned<I> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<I> Iterator for Cloned<I> where
I: Iterator,
I::Item: Deref,
<I::Item as Deref>::Target: Clone
impl<'a, I, T: 'a> Iterator for Cloned<I>
where I: Iterator<Item=&'a T>, T: Clone
{
type Item = <I::Item as Deref>::Target;
type Item = T;

fn next(&mut self) -> Option<<Self as Iterator>::Item> {
fn next(&mut self) -> Option<T> {
self.it.next().cloned()
}

Expand All @@ -1296,36 +1293,30 @@ impl<I> Iterator for Cloned<I> where
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<I> DoubleEndedIterator for Cloned<I> where
I: DoubleEndedIterator,
I::Item: Deref,
<I::Item as Deref>::Target: Clone
impl<'a, I, T: 'a> DoubleEndedIterator for Cloned<I>
where I: DoubleEndedIterator<Item=&'a T>, T: Clone
{
fn next_back(&mut self) -> Option<<Self as Iterator>::Item> {
fn next_back(&mut self) -> Option<T> {
self.it.next_back().cloned()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<I> ExactSizeIterator for Cloned<I> where
I: ExactSizeIterator,
I::Item: Deref,
<I::Item as Deref>::Target: Clone
impl<'a, I, T: 'a> ExactSizeIterator for Cloned<I>
where I: ExactSizeIterator<Item=&'a T>, T: Clone
{}

#[unstable(feature = "core", reason = "trait is experimental")]
impl<I> RandomAccessIterator for Cloned<I> where
I: RandomAccessIterator,
I::Item: Deref,
<I::Item as Deref>::Target: Clone
impl<'a, I, T: 'a> RandomAccessIterator for Cloned<I>
where I: RandomAccessIterator<Item=&'a T>, T: Clone
{
#[inline]
fn indexable(&self) -> usize {
self.it.indexable()
}

#[inline]
fn idx(&mut self, index: usize) -> Option<<Self as Iterator>::Item> {
fn idx(&mut self, index: usize) -> Option<T> {
self.it.idx(index).cloned()
}
}
Expand Down