Skip to content

Commit d264ef2

Browse files
committed
Rollup merge of rust-lang#22313 - japaric:iter, r=aturon
`IntoIterator` now has an extra associated item: ``` rust trait IntoIterator { type Item; type IntoIter: Iterator<Self=Self::Item>; } ``` This lets you bind the iterator \"`Item`\" directly when writing generic functions: ``` rust // hypothetical change, not included in this PR impl Extend<T> for Vec<T> { // you can now write fn extend<I>(&mut self, it: I) where I: IntoIterator<Item=T> { .. } // instead of fn extend<I: IntoIterator>(&mut self, it: I) where I::IntoIter: Iterator<Item=T> { .. } } ``` The downside is that now you have to write an extra associated type in your `IntoIterator` implementations: ``` diff impl<T> IntoIterator for Vec<T> { + type Item = T; type IntoIter = IntoIter<T>; fn into_iter(self) -> IntoIter<T> { .. } } ``` Because this breaks all downstream implementations of `IntoIterator`, this is a [breaking-change] --- r? @aturon
2 parents e337a57 + e727378 commit d264ef2

File tree

15 files changed

+441
-0
lines changed

15 files changed

+441
-0
lines changed

src/libcollections/binary_heap.rs

+24
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,8 @@ impl<T: Ord> FromIterator<T> for BinaryHeap<T> {
655655
}
656656
}
657657

658+
// NOTE(stage0): remove impl after a snapshot
659+
#[cfg(stage0)]
658660
impl<T: Ord> IntoIterator for BinaryHeap<T> {
659661
type IntoIter = IntoIter<T>;
660662

@@ -663,6 +665,18 @@ impl<T: Ord> IntoIterator for BinaryHeap<T> {
663665
}
664666
}
665667

668+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
669+
impl<T: Ord> IntoIterator for BinaryHeap<T> {
670+
type Item = T;
671+
type IntoIter = IntoIter<T>;
672+
673+
fn into_iter(self) -> IntoIter<T> {
674+
self.into_iter()
675+
}
676+
}
677+
678+
// NOTE(stage0): remove impl after a snapshot
679+
#[cfg(stage0)]
666680
impl<'a, T> IntoIterator for &'a BinaryHeap<T> where T: Ord {
667681
type IntoIter = Iter<'a, T>;
668682

@@ -671,6 +685,16 @@ impl<'a, T> IntoIterator for &'a BinaryHeap<T> where T: Ord {
671685
}
672686
}
673687

688+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
689+
impl<'a, T> IntoIterator for &'a BinaryHeap<T> where T: Ord {
690+
type Item = &'a T;
691+
type IntoIter = Iter<'a, T>;
692+
693+
fn into_iter(self) -> Iter<'a, T> {
694+
self.iter()
695+
}
696+
}
697+
674698
#[stable(feature = "rust1", since = "1.0.0")]
675699
impl<T: Ord> Extend<T> for BinaryHeap<T> {
676700
fn extend<Iter: Iterator<Item=T>>(&mut self, iter: Iter) {

src/libcollections/bit.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,8 @@ impl<'a> RandomAccessIterator for Iter<'a> {
10701070
}
10711071
}
10721072

1073+
// NOTE(stage0): remove impl after a snapshot
1074+
#[cfg(stage0)]
10731075
impl<'a> IntoIterator for &'a Bitv {
10741076
type IntoIter = Iter<'a>;
10751077

@@ -1078,6 +1080,16 @@ impl<'a> IntoIterator for &'a Bitv {
10781080
}
10791081
}
10801082

1083+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
1084+
impl<'a> IntoIterator for &'a Bitv {
1085+
type Item = bool;
1086+
type IntoIter = Iter<'a>;
1087+
1088+
fn into_iter(self) -> Iter<'a> {
1089+
self.iter()
1090+
}
1091+
}
1092+
10811093
/// An implementation of a set using a bit vector as an underlying
10821094
/// representation for holding unsigned numerical elements.
10831095
///
@@ -1882,6 +1894,8 @@ impl<'a> Iterator for SymmetricDifference<'a> {
18821894
#[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() }
18831895
}
18841896

1897+
// NOTE(stage0): remove impl after a snapshot
1898+
#[cfg(stage0)]
18851899
impl<'a> IntoIterator for &'a BitvSet {
18861900
type IntoIter = SetIter<'a>;
18871901

@@ -1890,6 +1904,16 @@ impl<'a> IntoIterator for &'a BitvSet {
18901904
}
18911905
}
18921906

1907+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
1908+
impl<'a> IntoIterator for &'a BitvSet {
1909+
type Item = usize;
1910+
type IntoIter = SetIter<'a>;
1911+
1912+
fn into_iter(self) -> SetIter<'a> {
1913+
self.iter()
1914+
}
1915+
}
1916+
18931917
#[cfg(test)]
18941918
mod tests {
18951919
use prelude::*;

src/libcollections/btree/map.rs

+36
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
462462
}
463463
}
464464

465+
// NOTE(stage0): remove impl after a snapshot
466+
#[cfg(stage0)]
465467
impl<K, V> IntoIterator for BTreeMap<K, V> {
466468
type IntoIter = IntoIter<K, V>;
467469

@@ -470,6 +472,18 @@ impl<K, V> IntoIterator for BTreeMap<K, V> {
470472
}
471473
}
472474

475+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
476+
impl<K, V> IntoIterator for BTreeMap<K, V> {
477+
type Item = (K, V);
478+
type IntoIter = IntoIter<K, V>;
479+
480+
fn into_iter(self) -> IntoIter<K, V> {
481+
self.into_iter()
482+
}
483+
}
484+
485+
// NOTE(stage0): remove impl after a snapshot
486+
#[cfg(stage0)]
473487
impl<'a, K, V> IntoIterator for &'a BTreeMap<K, V> {
474488
type IntoIter = Iter<'a, K, V>;
475489

@@ -478,6 +492,18 @@ impl<'a, K, V> IntoIterator for &'a BTreeMap<K, V> {
478492
}
479493
}
480494

495+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
496+
impl<'a, K, V> IntoIterator for &'a BTreeMap<K, V> {
497+
type Item = (&'a K, &'a V);
498+
type IntoIter = Iter<'a, K, V>;
499+
500+
fn into_iter(self) -> Iter<'a, K, V> {
501+
self.iter()
502+
}
503+
}
504+
505+
// NOTE(stage0): remove impl after a snapshot
506+
#[cfg(stage0)]
481507
impl<'a, K, V> IntoIterator for &'a mut BTreeMap<K, V> {
482508
type IntoIter = IterMut<'a, K, V>;
483509

@@ -486,6 +512,16 @@ impl<'a, K, V> IntoIterator for &'a mut BTreeMap<K, V> {
486512
}
487513
}
488514

515+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
516+
impl<'a, K, V> IntoIterator for &'a mut BTreeMap<K, V> {
517+
type Item = (&'a K, &'a mut V);
518+
type IntoIter = IterMut<'a, K, V>;
519+
520+
fn into_iter(mut self) -> IterMut<'a, K, V> {
521+
self.iter_mut()
522+
}
523+
}
524+
489525
/// A helper enum useful for deciding whether to continue a loop since we can't
490526
/// return from a closure
491527
enum Continuation<A, B> {

src/libcollections/btree/set.rs

+24
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,8 @@ impl<T: Ord> FromIterator<T> for BTreeSet<T> {
480480
}
481481
}
482482

483+
// NOTE(stage0): remove impl after a snapshot
484+
#[cfg(stage0)]
483485
impl<T> IntoIterator for BTreeSet<T> {
484486
type IntoIter = IntoIter<T>;
485487

@@ -488,6 +490,18 @@ impl<T> IntoIterator for BTreeSet<T> {
488490
}
489491
}
490492

493+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
494+
impl<T> IntoIterator for BTreeSet<T> {
495+
type Item = T;
496+
type IntoIter = IntoIter<T>;
497+
498+
fn into_iter(self) -> IntoIter<T> {
499+
self.into_iter()
500+
}
501+
}
502+
503+
// NOTE(stage0): remove impl after a snapshot
504+
#[cfg(stage0)]
491505
impl<'a, T> IntoIterator for &'a BTreeSet<T> {
492506
type IntoIter = Iter<'a, T>;
493507

@@ -496,6 +510,16 @@ impl<'a, T> IntoIterator for &'a BTreeSet<T> {
496510
}
497511
}
498512

513+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
514+
impl<'a, T> IntoIterator for &'a BTreeSet<T> {
515+
type Item = &'a T;
516+
type IntoIter = Iter<'a, T>;
517+
518+
fn into_iter(self) -> Iter<'a, T> {
519+
self.iter()
520+
}
521+
}
522+
499523
#[stable(feature = "rust1", since = "1.0.0")]
500524
impl<T: Ord> Extend<T> for BTreeSet<T> {
501525
#[inline]

src/libcollections/dlist.rs

+36
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,8 @@ impl<A> FromIterator<A> for DList<A> {
837837
}
838838
}
839839

840+
// NOTE(stage0): remove impl after a snapshot
841+
#[cfg(stage0)]
840842
impl<T> IntoIterator for DList<T> {
841843
type IntoIter = IntoIter<T>;
842844

@@ -845,15 +847,49 @@ impl<T> IntoIterator for DList<T> {
845847
}
846848
}
847849

850+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
851+
impl<T> IntoIterator for DList<T> {
852+
type Item = T;
853+
type IntoIter = IntoIter<T>;
854+
855+
fn into_iter(self) -> IntoIter<T> {
856+
self.into_iter()
857+
}
858+
}
859+
860+
// NOTE(stage0): remove impl after a snapshot
861+
#[cfg(stage0)]
862+
impl<'a, T> IntoIterator for &'a DList<T> {
863+
type IntoIter = Iter<'a, T>;
864+
865+
fn into_iter(self) -> Iter<'a, T> {
866+
self.iter()
867+
}
868+
}
869+
870+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
848871
impl<'a, T> IntoIterator for &'a DList<T> {
872+
type Item = &'a T;
849873
type IntoIter = Iter<'a, T>;
850874

851875
fn into_iter(self) -> Iter<'a, T> {
852876
self.iter()
853877
}
854878
}
855879

880+
// NOTE(stage0): remove impl after a snapshot
881+
#[cfg(stage0)]
882+
impl<'a, T> IntoIterator for &'a mut DList<T> {
883+
type IntoIter = IterMut<'a, T>;
884+
885+
fn into_iter(mut self) -> IterMut<'a, T> {
886+
self.iter_mut()
887+
}
888+
}
889+
890+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
856891
impl<'a, T> IntoIterator for &'a mut DList<T> {
892+
type Item = &'a mut T;
857893
type IntoIter = IterMut<'a, T>;
858894

859895
fn into_iter(mut self) -> IterMut<'a, T> {

src/libcollections/enum_set.rs

+12
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ impl<E:CLike> FromIterator<E> for EnumSet<E> {
257257
}
258258
}
259259

260+
// NOTE(stage0): remove impl after a snapshot
261+
#[cfg(stage0)]
260262
impl<'a, E> IntoIterator for &'a EnumSet<E> where E: CLike {
261263
type IntoIter = Iter<E>;
262264

@@ -265,6 +267,16 @@ impl<'a, E> IntoIterator for &'a EnumSet<E> where E: CLike {
265267
}
266268
}
267269

270+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
271+
impl<'a, E> IntoIterator for &'a EnumSet<E> where E: CLike {
272+
type Item = E;
273+
type IntoIter = Iter<E>;
274+
275+
fn into_iter(self) -> Iter<E> {
276+
self.iter()
277+
}
278+
}
279+
268280
impl<E:CLike> Extend<E> for EnumSet<E> {
269281
fn extend<I: Iterator<Item=E>>(&mut self, iterator: I) {
270282
for element in iterator {

src/libcollections/ring_buf.rs

+36
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,8 @@ impl<A> FromIterator<A> for RingBuf<A> {
16991699
}
17001700
}
17011701

1702+
// NOTE(stage0): remove impl after a snapshot
1703+
#[cfg(stage0)]
17021704
impl<T> IntoIterator for RingBuf<T> {
17031705
type IntoIter = IntoIter<T>;
17041706

@@ -1707,6 +1709,18 @@ impl<T> IntoIterator for RingBuf<T> {
17071709
}
17081710
}
17091711

1712+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
1713+
impl<T> IntoIterator for RingBuf<T> {
1714+
type Item = T;
1715+
type IntoIter = IntoIter<T>;
1716+
1717+
fn into_iter(self) -> IntoIter<T> {
1718+
self.into_iter()
1719+
}
1720+
}
1721+
1722+
// NOTE(stage0): remove impl after a snapshot
1723+
#[cfg(stage0)]
17101724
impl<'a, T> IntoIterator for &'a RingBuf<T> {
17111725
type IntoIter = Iter<'a, T>;
17121726

@@ -1715,6 +1729,18 @@ impl<'a, T> IntoIterator for &'a RingBuf<T> {
17151729
}
17161730
}
17171731

1732+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
1733+
impl<'a, T> IntoIterator for &'a RingBuf<T> {
1734+
type Item = &'a T;
1735+
type IntoIter = Iter<'a, T>;
1736+
1737+
fn into_iter(self) -> Iter<'a, T> {
1738+
self.iter()
1739+
}
1740+
}
1741+
1742+
// NOTE(stage0): remove impl after a snapshot
1743+
#[cfg(stage0)]
17181744
impl<'a, T> IntoIterator for &'a mut RingBuf<T> {
17191745
type IntoIter = IterMut<'a, T>;
17201746

@@ -1723,6 +1749,16 @@ impl<'a, T> IntoIterator for &'a mut RingBuf<T> {
17231749
}
17241750
}
17251751

1752+
#[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
1753+
impl<'a, T> IntoIterator for &'a mut RingBuf<T> {
1754+
type Item = &'a mut T;
1755+
type IntoIter = IterMut<'a, T>;
1756+
1757+
fn into_iter(mut self) -> IterMut<'a, T> {
1758+
self.iter_mut()
1759+
}
1760+
}
1761+
17261762
#[stable(feature = "rust1", since = "1.0.0")]
17271763
impl<A> Extend<A> for RingBuf<A> {
17281764
fn extend<T: Iterator<Item=A>>(&mut self, iterator: T) {

0 commit comments

Comments
 (0)