Skip to content

Commit fe955e7

Browse files
committed
iterator: add an Extendable trait
1 parent ffe549d commit fe955e7

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/libstd/iterator.rs

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ pub trait FromIterator<A, T: Iterator<A>> {
3232
fn from_iterator(iterator: &mut T) -> Self;
3333
}
3434

35+
/// A type growable from an `Iterator` implementation
36+
pub trait Extendable<A, T: Iterator<A>>: FromIterator<A, T> {
37+
/// Extend a container with the elements yielded by an iterator
38+
fn extend(&mut self, iterator: &mut T);
39+
}
40+
3541
/// An interface for dealing with "external iterators". These types of iterators
3642
/// can be resumed at any time as all state is stored internally as opposed to
3743
/// being located on the call stack.

src/libstd/vec.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -2224,7 +2224,7 @@ impl<T> Iterator<T> for VecConsumeRevIterator<T> {
22242224
}
22252225

22262226
impl<A, T: Iterator<A>> FromIterator<A, T> for ~[A] {
2227-
pub fn from_iterator(iterator: &mut T) -> ~[A] {
2227+
fn from_iterator(iterator: &mut T) -> ~[A] {
22282228
let (lower, _) = iterator.size_hint();
22292229
let mut xs = with_capacity(lower);
22302230
for iterator.advance |x| {
@@ -2234,6 +2234,17 @@ impl<A, T: Iterator<A>> FromIterator<A, T> for ~[A] {
22342234
}
22352235
}
22362236

2237+
impl<A, T: Iterator<A>> Extendable<A, T> for ~[A] {
2238+
fn extend(&mut self, iterator: &mut T) {
2239+
let (lower, _) = iterator.size_hint();
2240+
let len = self.len();
2241+
self.reserve(len + lower);
2242+
for iterator.advance |x| {
2243+
self.push(x);
2244+
}
2245+
}
2246+
}
2247+
22372248
#[cfg(test)]
22382249
mod tests {
22392250
use option::{None, Option, Some};

0 commit comments

Comments
 (0)