iterator: add a FromIterator trait

This is able to take advantage of the lower bound from the size hint.
This commit is contained in:
Daniel Micay
2013-06-21 07:57:22 -04:00
parent 468cbd9d01
commit c9342663df
2 changed files with 20 additions and 2 deletions

View File

@@ -19,7 +19,7 @@ use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater};
use clone::Clone;
use old_iter::BaseIter;
use old_iter;
use iterator::{Iterator, IteratorUtil};
use iterator::{FromIterator, Iterator, IteratorUtil};
use iter::FromIter;
use kinds::Copy;
use libc;
@@ -2511,6 +2511,18 @@ impl<T> FromIter<T> for ~[T]{
}
}
#[cfg(not(stage0))]
impl<A, T: Iterator<A>> FromIterator<A, T> for ~[A] {
pub fn from_iterator(iterator: &mut T) -> ~[A] {
let (lower, _) = iterator.size_hint();
let mut xs = with_capacity(lower.get_or_zero());
for iterator.advance |x| {
xs.push(x);
}
xs
}
}
#[cfg(test)]
mod tests {
use option::{None, Option, Some};