about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDaniel Micay <danielmicay@gmail.com>2013-05-18 04:32:53 -0400
committerDaniel Micay <danielmicay@gmail.com>2013-05-18 04:37:48 -0400
commit883d583faa9f33e69dce4b913f47a2d09e8b584d (patch)
tree1e4b0a02f5c70bb2956610714de05dbed912619b
parentea8a55b821460ef7b3b58052f99673674fca8f96 (diff)
downloadrust-883d583faa9f33e69dce4b913f47a2d09e8b584d.tar.gz
rust-883d583faa9f33e69dce4b913f47a2d09e8b584d.zip
iterator: reuse iter::to_vec, and use &mut self
-rw-r--r--src/libcore/iterator.rs11
1 files changed, 4 insertions, 7 deletions
diff --git a/src/libcore/iterator.rs b/src/libcore/iterator.rs
index 65c36b122c7..0b0df4a5d70 100644
--- a/src/libcore/iterator.rs
+++ b/src/libcore/iterator.rs
@@ -47,7 +47,7 @@ pub trait IteratorUtil<A> {
     fn advance(&mut self, f: &fn(A) -> bool);
     #[cfg(not(stage0))]
     fn advance(&mut self, f: &fn(A) -> bool) -> bool;
-    fn to_vec(self) -> ~[A];
+    fn to_vec(&mut self) -> ~[A];
     fn nth(&mut self, n: uint) -> Option<A>;
     fn last(&mut self) -> Option<A>;
     fn fold<B>(&mut self, start: B, f: &fn(B, A) -> B) -> B;
@@ -147,11 +147,8 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
     }
 
     #[inline(always)]
-    fn to_vec(self) -> ~[A] {
-        let mut v = ~[];
-        let mut it = self;
-        for it.advance() |x| { v.push(x); }
-        return v;
+    fn to_vec(&mut self) -> ~[A] {
+        iter::to_vec::<A>(|f| self.advance(f))
     }
 
     /// Return the `n`th item yielded by an iterator.
@@ -563,7 +560,7 @@ mod tests {
 
     #[test]
     fn test_filter_map() {
-        let it  = Counter::new(0u, 1u).take(10)
+        let mut it = Counter::new(0u, 1u).take(10)
             .filter_map(|x: uint| if x.is_even() { Some(x*x) } else { None });
         assert_eq!(it.to_vec(), ~[0*0, 2*2, 4*4, 6*6, 8*8]);
     }