Skip to content

Commit 05354e8

Browse files
committed
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.
2 parents 48df3fb + 7c333e9 commit 05354e8

File tree

2 files changed

+19
-28
lines changed

2 files changed

+19
-28
lines changed

src/libcollectionstest/vec_deque.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ fn test_mut_rev_iter_wrap() {
360360
assert_eq!(d.pop_front(), Some(1));
361361
d.push_back(4);
362362

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

src/libcore/iter.rs

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use default::Default;
6565
use marker;
6666
use mem;
6767
use num::{ToPrimitive, Int};
68-
use ops::{Add, Deref, FnMut, RangeFrom};
68+
use ops::{Add, FnMut, RangeFrom};
6969
use option::Option;
7070
use option::Option::{Some, None};
7171
use marker::Sized;
@@ -976,12 +976,11 @@ pub trait IteratorExt: Iterator + Sized {
976976
(ts, us)
977977
}
978978

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

12811280
#[stable(feature = "rust1", since = "1.0.0")]
1282-
impl<I> Iterator for Cloned<I> where
1283-
I: Iterator,
1284-
I::Item: Deref,
1285-
<I::Item as Deref>::Target: Clone
1281+
impl<'a, I, T: 'a> Iterator for Cloned<I>
1282+
where I: Iterator<Item=&'a T>, T: Clone
12861283
{
1287-
type Item = <I::Item as Deref>::Target;
1284+
type Item = T;
12881285

1289-
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
1286+
fn next(&mut self) -> Option<T> {
12901287
self.it.next().cloned()
12911288
}
12921289

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

12981295
#[stable(feature = "rust1", since = "1.0.0")]
1299-
impl<I> DoubleEndedIterator for Cloned<I> where
1300-
I: DoubleEndedIterator,
1301-
I::Item: Deref,
1302-
<I::Item as Deref>::Target: Clone
1296+
impl<'a, I, T: 'a> DoubleEndedIterator for Cloned<I>
1297+
where I: DoubleEndedIterator<Item=&'a T>, T: Clone
13031298
{
1304-
fn next_back(&mut self) -> Option<<Self as Iterator>::Item> {
1299+
fn next_back(&mut self) -> Option<T> {
13051300
self.it.next_back().cloned()
13061301
}
13071302
}
13081303

13091304
#[stable(feature = "rust1", since = "1.0.0")]
1310-
impl<I> ExactSizeIterator for Cloned<I> where
1311-
I: ExactSizeIterator,
1312-
I::Item: Deref,
1313-
<I::Item as Deref>::Target: Clone
1305+
impl<'a, I, T: 'a> ExactSizeIterator for Cloned<I>
1306+
where I: ExactSizeIterator<Item=&'a T>, T: Clone
13141307
{}
13151308

13161309
#[unstable(feature = "core", reason = "trait is experimental")]
1317-
impl<I> RandomAccessIterator for Cloned<I> where
1318-
I: RandomAccessIterator,
1319-
I::Item: Deref,
1320-
<I::Item as Deref>::Target: Clone
1310+
impl<'a, I, T: 'a> RandomAccessIterator for Cloned<I>
1311+
where I: RandomAccessIterator<Item=&'a T>, T: Clone
13211312
{
13221313
#[inline]
13231314
fn indexable(&self) -> usize {
13241315
self.it.indexable()
13251316
}
13261317

13271318
#[inline]
1328-
fn idx(&mut self, index: usize) -> Option<<Self as Iterator>::Item> {
1319+
fn idx(&mut self, index: usize) -> Option<T> {
13291320
self.it.idx(index).cloned()
13301321
}
13311322
}

0 commit comments

Comments
 (0)