From f7e58ebe84513ed65b1694311f8c4f35e53e8c0e Mon Sep 17 00:00:00 2001 From: gifnksm Date: Fri, 17 May 2013 22:54:32 +0900 Subject: [PATCH] libcore: Add `IteratorUtil::to_vec()` method --- src/libcore/iterator.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/libcore/iterator.rs b/src/libcore/iterator.rs index 40c9637f692..3811d28b030 100644 --- a/src/libcore/iterator.rs +++ b/src/libcore/iterator.rs @@ -45,6 +45,7 @@ fn scan<'r, St, B>(self, initial_state: St, f: &'r fn(&mut St, A) -> Option) 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]; } /// Iterator adaptors provided for every `Iterator` implementation. The adaptor objects are also @@ -131,6 +132,14 @@ fn advance(&mut self, f: &fn(A) -> bool) -> bool { } } } + + #[inline(always)] + fn to_vec(self) -> ~[A] { + let mut v = ~[]; + let mut it = self; + for it.advance() |x| { v.push(x); } + return v; + } } pub struct ChainIterator {