Skip to content

Commit 1a44875

Browse files
committed
auto merge of #19176 : aturon/rust/stab-iter, r=alexcrichton
This is an initial pass at stabilizing the `iter` module. The module is fairly large, but is also pretty polished, so most of the stabilization leaves things as they are. Some changes: * Due to the new object safety rules, various traits needs to be split into object-safe traits and extension traits. This includes `Iterator` itself. While splitting up the traits adds some complexity, it will also increase flexbility: once we have automatic impls of `Trait` for trait objects over `Trait`, then things like the iterator adapters will all work with trait objects. * Iterator adapters that use up the entire iterator now take it by value, which makes the semantics more clear and helps catch bugs. Due to the splitting of Iterator, this does not affect trait objects. If the underlying iterator is still desired for some reason, `by_ref` can be used. (Note: this change had no fallout in the Rust distro except for the useless mut lint.) * In general, extension traits new and old are following an [in-progress convention](rust-lang/rfcs#445). As such, they are marked `unstable`. * As usual, anything involving closures is `unstable` pending unboxed closures. * A few of the more esoteric/underdeveloped iterator forms (like `RandomAccessIterator` and `MutableDoubleEndedIterator`, along with various unfolds) are left experimental for now. * The `order` submodule is left `experimental` because it will hopefully be replaced by generalized comparison traits. * "Leaf" iterators (like `Repeat` and `Counter`) are uniformly constructed by free fns at the module level. That's because the types are not otherwise of any significance (if we had `impl Trait`, you wouldn't want to define a type at all). Closes #17701 Due to renamings and splitting of traits, this is a: [breaking-change]
2 parents 930f877 + b299c2b commit 1a44875

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+304
-147
lines changed

src/libcollections/binary_heap.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -585,10 +585,10 @@ impl<T> DoubleEndedIterator<T> for MoveItems<T> {
585585
fn next_back(&mut self) -> Option<T> { self.iter.next_back() }
586586
}
587587

588-
impl<T> ExactSize<T> for MoveItems<T> {}
588+
impl<T> ExactSizeIterator<T> for MoveItems<T> {}
589589

590590
impl<T: Ord> FromIterator<T> for BinaryHeap<T> {
591-
fn from_iter<Iter: Iterator<T>>(mut iter: Iter) -> BinaryHeap<T> {
591+
fn from_iter<Iter: Iterator<T>>(iter: Iter) -> BinaryHeap<T> {
592592
let vec: Vec<T> = iter.collect();
593593
BinaryHeap::from_vec(vec)
594594
}

src/libcollections/bit.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ use core::prelude::*;
6868
use core::cmp;
6969
use core::default::Default;
7070
use core::fmt;
71-
use core::iter::{Chain, Enumerate, Repeat, Skip, Take};
71+
use core::iter::{Chain, Enumerate, Repeat, Skip, Take, repeat};
7272
use core::iter;
7373
use core::num::Int;
7474
use core::slice;
@@ -88,11 +88,11 @@ fn match_words <'a,'b>(a: &'a Bitv, b: &'b Bitv) -> (MatchWords<'a>, MatchWords<
8888

8989
// have to uselessly pretend to pad the longer one for type matching
9090
if a_len < b_len {
91-
(a.mask_words(0).chain(Repeat::new(0u32).enumerate().take(b_len).skip(a_len)),
92-
b.mask_words(0).chain(Repeat::new(0u32).enumerate().take(0).skip(0)))
91+
(a.mask_words(0).chain(repeat(0u32).enumerate().take(b_len).skip(a_len)),
92+
b.mask_words(0).chain(repeat(0u32).enumerate().take(0).skip(0)))
9393
} else {
94-
(a.mask_words(0).chain(Repeat::new(0u32).enumerate().take(0).skip(0)),
95-
b.mask_words(0).chain(Repeat::new(0u32).enumerate().take(a_len).skip(b_len)))
94+
(a.mask_words(0).chain(repeat(0u32).enumerate().take(0).skip(0)),
95+
b.mask_words(0).chain(repeat(0u32).enumerate().take(a_len).skip(b_len)))
9696
}
9797
}
9898

@@ -943,7 +943,7 @@ impl<'a> DoubleEndedIterator<bool> for Bits<'a> {
943943
}
944944
}
945945

946-
impl<'a> ExactSize<bool> for Bits<'a> {}
946+
impl<'a> ExactSizeIterator<bool> for Bits<'a> {}
947947

948948
impl<'a> RandomAccessIterator<bool> for Bits<'a> {
949949
#[inline]

src/libcollections/btree/map.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ impl<K, V, E, T: Traverse<E> + DoubleEndedIterator<TraversalItem<K, V, E>>>
863863
// Note that the design of these iterators permits an *arbitrary* initial pair of min and max,
864864
// making these arbitrary sub-range iterators. However the logic to construct these paths
865865
// efficiently is fairly involved, so this is a FIXME. The sub-range iterators also wouldn't be
866-
// able to accurately predict size, so those iterators can't implement ExactSize.
866+
// able to accurately predict size, so those iterators can't implement ExactSizeIterator.
867867
fn next(&mut self) -> Option<(K, V)> {
868868
loop {
869869
// We want the smallest element, so try to get the top of the left stack
@@ -963,7 +963,7 @@ impl<'a, K, V> Iterator<(&'a K, &'a V)> for Entries<'a, K, V> {
963963
impl<'a, K, V> DoubleEndedIterator<(&'a K, &'a V)> for Entries<'a, K, V> {
964964
fn next_back(&mut self) -> Option<(&'a K, &'a V)> { self.inner.next_back() }
965965
}
966-
impl<'a, K, V> ExactSize<(&'a K, &'a V)> for Entries<'a, K, V> {}
966+
impl<'a, K, V> ExactSizeIterator<(&'a K, &'a V)> for Entries<'a, K, V> {}
967967

968968

969969
impl<'a, K, V> Iterator<(&'a K, &'a mut V)> for MutEntries<'a, K, V> {
@@ -973,7 +973,7 @@ impl<'a, K, V> Iterator<(&'a K, &'a mut V)> for MutEntries<'a, K, V> {
973973
impl<'a, K, V> DoubleEndedIterator<(&'a K, &'a mut V)> for MutEntries<'a, K, V> {
974974
fn next_back(&mut self) -> Option<(&'a K, &'a mut V)> { self.inner.next_back() }
975975
}
976-
impl<'a, K, V> ExactSize<(&'a K, &'a mut V)> for MutEntries<'a, K, V> {}
976+
impl<'a, K, V> ExactSizeIterator<(&'a K, &'a mut V)> for MutEntries<'a, K, V> {}
977977

978978

979979
impl<K, V> Iterator<(K, V)> for MoveEntries<K, V> {
@@ -983,7 +983,7 @@ impl<K, V> Iterator<(K, V)> for MoveEntries<K, V> {
983983
impl<K, V> DoubleEndedIterator<(K, V)> for MoveEntries<K, V> {
984984
fn next_back(&mut self) -> Option<(K, V)> { self.inner.next_back() }
985985
}
986-
impl<K, V> ExactSize<(K, V)> for MoveEntries<K, V> {}
986+
impl<K, V> ExactSizeIterator<(K, V)> for MoveEntries<K, V> {}
987987

988988

989989

src/libcollections/dlist.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ impl<'a, A> DoubleEndedIterator<&'a A> for Items<'a, A> {
607607
}
608608
}
609609

610-
impl<'a, A> ExactSize<&'a A> for Items<'a, A> {}
610+
impl<'a, A> ExactSizeIterator<&'a A> for Items<'a, A> {}
611611

612612
impl<'a, A> Iterator<&'a mut A> for MutItems<'a, A> {
613613
#[inline]
@@ -645,7 +645,7 @@ impl<'a, A> DoubleEndedIterator<&'a mut A> for MutItems<'a, A> {
645645
}
646646
}
647647

648-
impl<'a, A> ExactSize<&'a mut A> for MutItems<'a, A> {}
648+
impl<'a, A> ExactSizeIterator<&'a mut A> for MutItems<'a, A> {}
649649

650650
/// Allows mutating a `DList` while iterating.
651651
pub trait ListInsertion<A> {

src/libcollections/ring_buf.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,7 @@ impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> {
695695
}
696696
}
697697

698-
699-
impl<'a, T> ExactSize<&'a T> for Items<'a, T> {}
698+
impl<'a, T> ExactSizeIterator<&'a T> for Items<'a, T> {}
700699

701700
impl<'a, T> RandomAccessIterator<&'a T> for Items<'a, T> {
702701
#[inline]
@@ -763,7 +762,7 @@ impl<'a, T> DoubleEndedIterator<&'a mut T> for MutItems<'a, T> {
763762
}
764763
}
765764

766-
impl<'a, T> ExactSize<&'a mut T> for MutItems<'a, T> {}
765+
impl<'a, T> ExactSizeIterator<&'a mut T> for MutItems<'a, T> {}
767766

768767
impl<A: PartialEq> PartialEq for RingBuf<A> {
769768
fn eq(&self, other: &RingBuf<A>) -> bool {
@@ -1322,7 +1321,7 @@ mod tests {
13221321
let u: Vec<int> = deq.iter().map(|&x| x).collect();
13231322
assert_eq!(u, v);
13241323

1325-
let mut seq = iter::count(0u, 2).take(256);
1324+
let seq = iter::count(0u, 2).take(256);
13261325
let deq: RingBuf<uint> = seq.collect();
13271326
for (i, &x) in deq.iter().enumerate() {
13281327
assert_eq!(2*i, x);

src/libcollections/slice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ use core::cmp;
9494
use core::kinds::Sized;
9595
use core::mem::size_of;
9696
use core::mem;
97-
use core::prelude::{Clone, Greater, Iterator, Less, None, Option};
97+
use core::prelude::{Clone, Greater, Iterator, IteratorExt, Less, None, Option};
9898
use core::prelude::{Ord, Ordering, RawPtr, Some, range};
9999
use core::ptr;
100100
use core::iter::{range_step, MultiplicativeIterator};

src/libcollections/str.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use core::cmp;
6161
use core::iter::AdditiveIterator;
6262
use core::kinds::Sized;
6363
use core::prelude::{Char, Clone, Eq, Equiv};
64-
use core::prelude::{Iterator, SlicePrelude, None, Option, Ord, Ordering};
64+
use core::prelude::{Iterator, IteratorExt, SlicePrelude, None, Option, Ord, Ordering};
6565
use core::prelude::{PartialEq, PartialOrd, Result, AsSlice, Some, Tuple2};
6666
use core::prelude::{range};
6767

@@ -828,7 +828,7 @@ mod tests {
828828
use std::cmp::{Equal, Greater, Less, Ord, PartialOrd, Equiv};
829829
use std::option::{Some, None};
830830
use std::ptr::RawPtr;
831-
use std::iter::{Iterator, DoubleEndedIterator};
831+
use std::iter::{Iterator, IteratorExt, DoubleEndedIteratorExt};
832832

833833
use super::*;
834834
use std::slice::{AsSlice, SlicePrelude};
@@ -2177,12 +2177,15 @@ mod tests {
21772177
let gr_inds = s.grapheme_indices(true).rev().collect::<Vec<(uint, &str)>>();
21782178
let b: &[_] = &[(11, "\r\n"), (6, "ö̲"), (3, "é"), (0u, "a̐")];
21792179
assert_eq!(gr_inds.as_slice(), b);
2180-
let mut gr_inds = s.grapheme_indices(true);
2181-
let e1 = gr_inds.size_hint();
2182-
assert_eq!(e1, (1, Some(13)));
2183-
let c = gr_inds.count();
2184-
assert_eq!(c, 4);
2185-
let e2 = gr_inds.size_hint();
2180+
let mut gr_inds_iter = s.grapheme_indices(true);
2181+
{
2182+
let gr_inds = gr_inds_iter.by_ref();
2183+
let e1 = gr_inds.size_hint();
2184+
assert_eq!(e1, (1, Some(13)));
2185+
let c = gr_inds.count();
2186+
assert_eq!(c, 4);
2187+
}
2188+
let e2 = gr_inds_iter.size_hint();
21862189
assert_eq!(e2, (0, Some(0)));
21872190

21882191
// make sure the reverse iterator does the right thing with "\n" at beginning of string
@@ -2319,7 +2322,7 @@ mod bench {
23192322
use test::Bencher;
23202323
use test::black_box;
23212324
use super::*;
2322-
use std::iter::{Iterator, DoubleEndedIterator};
2325+
use std::iter::{IteratorExt, DoubleEndedIteratorExt};
23232326
use std::str::StrPrelude;
23242327
use std::slice::SlicePrelude;
23252328

src/libcollections/vec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ impl<T> DoubleEndedIterator<T> for MoveItems<T> {
13411341
}
13421342
}
13431343

1344-
impl<T> ExactSize<T> for MoveItems<T> {}
1344+
impl<T> ExactSizeIterator<T> for MoveItems<T> {}
13451345

13461346
#[unsafe_destructor]
13471347
impl<T> Drop for MoveItems<T> {

src/libcore/fmt/float.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub use self::SignFormat::*;
1717
use char;
1818
use char::Char;
1919
use fmt;
20-
use iter::{range, DoubleEndedIterator};
20+
use iter::{range, DoubleEndedIteratorExt};
2121
use num::{Float, FPNaN, FPInfinite, ToPrimitive};
2222
use num::cast;
2323
use result::Ok;

src/libcore/fmt/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use any;
1616
use cell::{Cell, Ref, RefMut};
17-
use iter::{Iterator, range};
17+
use iter::{Iterator, IteratorExt, range};
1818
use kinds::{Copy, Sized};
1919
use mem;
2020
use option::{Option, Some, None};

src/libcore/fmt/num.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#![allow(unsigned_negation)]
1616

1717
use fmt;
18-
use iter::DoubleEndedIterator;
18+
use iter::DoubleEndedIteratorExt;
1919
use num::{Int, cast};
2020
use slice::SlicePrelude;
2121

0 commit comments

Comments
 (0)