Skip to content

Commit 1452797

Browse files
committed
auto merge of #6980 : Kimundi/rust/iterator-collect3, r=thestinger
2 parents 533425e + cac4891 commit 1452797

File tree

5 files changed

+56
-38
lines changed

5 files changed

+56
-38
lines changed

src/librustc/util/enum_set.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,19 +207,19 @@ mod test {
207207
fn test_each() {
208208
let mut e1: EnumSet<Foo> = EnumSet::empty();
209209

210-
assert_eq!(~[], iter::to_vec(|f| e1.each(f)))
210+
assert_eq!(~[], iter::FromIter::from_iter::<Foo, ~[Foo]>(|f| e1.each(f)))
211211

212212
e1.add(A);
213-
assert_eq!(~[A], iter::to_vec(|f| e1.each(f)))
213+
assert_eq!(~[A], iter::FromIter::from_iter::<Foo, ~[Foo]>(|f| e1.each(f)))
214214

215215
e1.add(C);
216-
assert_eq!(~[A,C], iter::to_vec(|f| e1.each(f)))
216+
assert_eq!(~[A,C], iter::FromIter::from_iter::<Foo, ~[Foo]>(|f| e1.each(f)))
217217

218218
e1.add(C);
219-
assert_eq!(~[A,C], iter::to_vec(|f| e1.each(f)))
219+
assert_eq!(~[A,C], iter::FromIter::from_iter::<Foo, ~[Foo]>(|f| e1.each(f)))
220220

221221
e1.add(B);
222-
assert_eq!(~[A,B,C], iter::to_vec(|f| e1.each(f)))
222+
assert_eq!(~[A,B,C], iter::FromIter::from_iter::<Foo, ~[Foo]>(|f| e1.each(f)))
223223
}
224224

225225
///////////////////////////////////////////////////////////////////////////
@@ -236,12 +236,12 @@ mod test {
236236
e2.add(C);
237237

238238
let e_union = e1 | e2;
239-
assert_eq!(~[A,B,C], iter::to_vec(|f| e_union.each(f)))
239+
assert_eq!(~[A,B,C], iter::FromIter::from_iter::<Foo, ~[Foo]>(|f| e_union.each(f)))
240240

241241
let e_intersection = e1 & e2;
242-
assert_eq!(~[C], iter::to_vec(|f| e_intersection.each(f)))
242+
assert_eq!(~[C], iter::FromIter::from_iter::<Foo, ~[Foo]>(|f| e_intersection.each(f)))
243243

244244
let e_subtract = e1 - e2;
245-
assert_eq!(~[A], iter::to_vec(|f| e_subtract.each(f)))
245+
assert_eq!(~[A], iter::FromIter::from_iter::<Foo, ~[Foo]>(|f| e_subtract.each(f)))
246246
}
247247
}

src/libstd/iter.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,18 @@ pub trait Times {
5151
fn times(&self, it: &fn() -> bool) -> bool;
5252
}
5353

54-
/**
55-
* Transform an internal iterator into an owned vector.
56-
*
57-
* # Example:
58-
*
59-
* ~~~ {.rust}
60-
* let xs = ~[1, 2, 3];
61-
* let ys = do iter::to_vec |f| { xs.each(|x| f(*x)) };
62-
* assert_eq!(xs, ys);
63-
* ~~~
64-
*/
65-
#[inline(always)]
66-
pub fn to_vec<T>(iter: &fn(f: &fn(T) -> bool) -> bool) -> ~[T] {
67-
let mut v = ~[];
68-
for iter |x| { v.push(x) }
69-
v
54+
#[allow(missing_doc)]
55+
pub trait FromIter<T> {
56+
/// Build a container with elements from an internal iterator.
57+
///
58+
/// # Example:
59+
///
60+
/// ~~~ {.rust}
61+
/// let xs = ~[1, 2, 3];
62+
/// let ys: ~[int] = do FromIter::from_iter |f| { xs.each(|x| f(*x)) };
63+
/// assert_eq!(xs, ys);
64+
/// ~~~
65+
pub fn from_iter(iter: &fn(f: &fn(T) -> bool) -> bool) -> Self;
7066
}
7167

7268
/**
@@ -262,9 +258,9 @@ mod tests {
262258
use uint;
263259

264260
#[test]
265-
fn test_to_vec() {
261+
fn test_from_iter() {
266262
let xs = ~[1, 2, 3];
267-
let ys = do to_vec |f| { xs.each(|x| f(*x)) };
263+
let ys: ~[int] = do FromIter::from_iter |f| { xs.each(|x| f(*x)) };
268264
assert_eq!(xs, ys);
269265
}
270266

src/libstd/iterator.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ implementing the `Iterator` trait.
1919

2020
use cmp;
2121
use iter;
22+
use iter::{FromIter, Times};
2223
use num::{Zero, One};
23-
use prelude::*;
24+
use option::{Option, Some, None};
25+
use ops::{Add, Mul};
26+
use cmp::Ord;
27+
use clone::Clone;
2428

2529
/// An interface for dealing with "external iterators". These types of iterators
2630
/// can be resumed at any time as all state is stored internally as opposed to
@@ -241,19 +245,19 @@ pub trait IteratorUtil<A> {
241245
/// ~~~
242246
fn advance(&mut self, f: &fn(A) -> bool) -> bool;
243247

244-
/// Loops through the entire iterator, accumulating all of the elements into
245-
/// a vector.
248+
/// Loops through the entire iterator, collecting all of the elements into
249+
/// a container implementing `FromIter`.
246250
///
247251
/// # Example
248252
///
249253
/// ~~~ {.rust}
250254
/// use std::iterator::*;
251255
///
252256
/// let a = [1, 2, 3, 4, 5];
253-
/// let b = a.iter().transform(|&x| x).to_vec();
257+
/// let b: ~[int] = a.iter().transform(|&x| x).collect();
254258
/// assert!(a == b);
255259
/// ~~~
256-
fn to_vec(&mut self) -> ~[A];
260+
fn collect<B: FromIter<A>>(&mut self) -> B;
257261

258262
/// Loops through `n` iterations, returning the `n`th element of the
259263
/// iterator.
@@ -415,8 +419,8 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
415419
}
416420

417421
#[inline(always)]
418-
fn to_vec(&mut self) -> ~[A] {
419-
iter::to_vec::<A>(|f| self.advance(f))
422+
fn collect<B: FromIter<A>>(&mut self) -> B {
423+
FromIter::from_iter::<A, B>(|f| self.advance(f))
420424
}
421425

422426
/// Return the `n`th item yielded by an iterator.
@@ -870,9 +874,9 @@ mod tests {
870874
use uint;
871875

872876
#[test]
873-
fn test_counter_to_vec() {
877+
fn test_counter_from_iter() {
874878
let mut it = Counter::new(0, 5).take(10);
875-
let xs = iter::to_vec(|f| it.advance(f));
879+
let xs: ~[int] = iter::FromIter::from_iter::<int, ~[int]>(|f| it.advance(f));
876880
assert_eq!(xs, ~[0, 5, 10, 15, 20, 25, 30, 35, 40, 45]);
877881
}
878882

@@ -903,7 +907,7 @@ mod tests {
903907
fn test_filter_map() {
904908
let mut it = Counter::new(0u, 1u).take(10)
905909
.filter_map(|x: uint| if x.is_even() { Some(x*x) } else { None });
906-
assert_eq!(it.to_vec(), ~[0*0, 2*2, 4*4, 6*6, 8*8]);
910+
assert_eq!(it.collect::<~[uint]>(), ~[0*0, 2*2, 4*4, 6*6, 8*8]);
907911
}
908912

909913
#[test]
@@ -1062,6 +1066,13 @@ mod tests {
10621066
assert_eq!(v.slice(0, 0).iter().transform(|&x| x).min(), None);
10631067
}
10641068

1069+
#[test]
1070+
fn test_collect() {
1071+
let a = ~[1, 2, 3, 4, 5];
1072+
let b: ~[int] = a.iter().transform(|&x| x).collect();
1073+
assert_eq!(a, b);
1074+
}
1075+
10651076
#[test]
10661077
fn test_all() {
10671078
let v = ~&[1, 2, 3, 4, 5];

src/libstd/prelude.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Many programming languages have a 'prelude': a particular subset of the
1414
libraries that come with the language. Every program imports the prelude by
1515
default.
1616
17-
For example, it would be annoying to add `use io::println;` to every single
17+
For example, it would be annoying to add `use std::io::println;` to every single
1818
program, and the vast majority of Rust programs will wish to print to standard
1919
output. Therefore, it makes sense to import it into every program.
2020
@@ -49,7 +49,8 @@ pub use hash::Hash;
4949
pub use old_iter::{BaseIter, ReverseIter, MutableIter, ExtendedIter, EqIter};
5050
pub use old_iter::{CopyableIter, CopyableOrderedIter, CopyableNonstrictIter};
5151
pub use old_iter::{ExtendedMutableIter};
52-
pub use iter::Times;
52+
pub use iter::{Times, FromIter};
53+
// FIXME: #5898 pub use iterator::{Iterator, IteratorUtil};
5354
pub use num::{Num, NumCast};
5455
pub use num::{Orderable, Signed, Unsigned, Round};
5556
pub use num::{Algebraic, Trigonometric, Exponential, Hyperbolic};

src/libstd/vec.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use clone::Clone;
2020
use old_iter::BaseIter;
2121
use old_iter;
2222
use iterator::Iterator;
23+
use iter::FromIter;
2324
use kinds::Copy;
2425
use libc;
2526
use old_iter::CopyableIter;
@@ -2996,6 +2997,15 @@ impl<'self, T> Iterator<&'self mut T> for MutVecIterator<'self, T> {
29962997
}
29972998
}
29982999

3000+
impl<T> FromIter<T> for ~[T]{
3001+
#[inline(always)]
3002+
pub fn from_iter(iter: &fn(f: &fn(T) -> bool) -> bool) -> ~[T] {
3003+
let mut v = ~[];
3004+
for iter |x| { v.push(x) }
3005+
v
3006+
}
3007+
}
3008+
29993009
#[cfg(test)]
30003010
mod tests {
30013011
use option::{None, Option, Some};

0 commit comments

Comments
 (0)