Rollup merge of #23462 - alexcrichton:stabilize-cloned, r=aturon
This commit stabilizes the `cloned` iterator after tweaking the signature to require that the iterator is over `&T` instead of `U: Deref<T>`. This method has had time to bake for awhile now and it's not clear whether the `Deref` bound is worth it. Additionally, there aren't clear conventions on when to bound and/or implement the `Deref` trait, so for now the conservative route is to require references instead of `U: Deref<T>`. To change this signature to using `Deref` would technically be a backwards-incompatible change, but it is doubtful that any code will actually break in practice.
This commit is contained in:
commit
05354e8522
2 changed files with 19 additions and 28 deletions
|
@ -360,7 +360,7 @@ fn test_mut_rev_iter_wrap() {
|
||||||
assert_eq!(d.pop_front(), Some(1));
|
assert_eq!(d.pop_front(), Some(1));
|
||||||
d.push_back(4);
|
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]);
|
vec![4, 3, 2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -65,7 +65,7 @@ use default::Default;
|
||||||
use marker;
|
use marker;
|
||||||
use mem;
|
use mem;
|
||||||
use num::{ToPrimitive, Int};
|
use num::{ToPrimitive, Int};
|
||||||
use ops::{Add, Deref, FnMut, RangeFrom};
|
use ops::{Add, FnMut, RangeFrom};
|
||||||
use option::Option;
|
use option::Option;
|
||||||
use option::Option::{Some, None};
|
use option::Option::{Some, None};
|
||||||
use marker::Sized;
|
use marker::Sized;
|
||||||
|
@ -976,12 +976,11 @@ pub trait IteratorExt: Iterator + Sized {
|
||||||
(ts, us)
|
(ts, us)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an iterator that clones the elements it yields. Useful for converting an
|
/// Creates an iterator that clones the elements it yields. Useful for
|
||||||
/// Iterator<&T> to an Iterator<T>.
|
/// converting an Iterator<&T> to an Iterator<T>.
|
||||||
#[unstable(feature = "core", reason = "recent addition")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
fn cloned(self) -> Cloned<Self> where
|
fn cloned<'a, T: 'a>(self) -> Cloned<Self>
|
||||||
Self::Item: Deref,
|
where Self: Iterator<Item=&'a T>, T: Clone
|
||||||
<Self::Item as Deref>::Target: Clone,
|
|
||||||
{
|
{
|
||||||
Cloned { it: self }
|
Cloned { it: self }
|
||||||
}
|
}
|
||||||
|
@ -1279,14 +1278,12 @@ pub struct Cloned<I> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl<I> Iterator for Cloned<I> where
|
impl<'a, I, T: 'a> Iterator for Cloned<I>
|
||||||
I: Iterator,
|
where I: Iterator<Item=&'a T>, T: Clone
|
||||||
I::Item: Deref,
|
|
||||||
<I::Item as Deref>::Target: 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()
|
self.it.next().cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1296,28 +1293,22 @@ impl<I> Iterator for Cloned<I> where
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl<I> DoubleEndedIterator for Cloned<I> where
|
impl<'a, I, T: 'a> DoubleEndedIterator for Cloned<I>
|
||||||
I: DoubleEndedIterator,
|
where I: DoubleEndedIterator<Item=&'a T>, T: Clone
|
||||||
I::Item: Deref,
|
|
||||||
<I::Item as Deref>::Target: Clone
|
|
||||||
{
|
{
|
||||||
fn next_back(&mut self) -> Option<<Self as Iterator>::Item> {
|
fn next_back(&mut self) -> Option<T> {
|
||||||
self.it.next_back().cloned()
|
self.it.next_back().cloned()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl<I> ExactSizeIterator for Cloned<I> where
|
impl<'a, I, T: 'a> ExactSizeIterator for Cloned<I>
|
||||||
I: ExactSizeIterator,
|
where I: ExactSizeIterator<Item=&'a T>, T: Clone
|
||||||
I::Item: Deref,
|
|
||||||
<I::Item as Deref>::Target: Clone
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
#[unstable(feature = "core", reason = "trait is experimental")]
|
#[unstable(feature = "core", reason = "trait is experimental")]
|
||||||
impl<I> RandomAccessIterator for Cloned<I> where
|
impl<'a, I, T: 'a> RandomAccessIterator for Cloned<I>
|
||||||
I: RandomAccessIterator,
|
where I: RandomAccessIterator<Item=&'a T>, T: Clone
|
||||||
I::Item: Deref,
|
|
||||||
<I::Item as Deref>::Target: Clone
|
|
||||||
{
|
{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn indexable(&self) -> usize {
|
fn indexable(&self) -> usize {
|
||||||
|
@ -1325,7 +1316,7 @@ impl<I> RandomAccessIterator for Cloned<I> where
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[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()
|
self.it.idx(index).cloned()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue