diff --git a/src/libextra/dlist.rs b/src/libextra/dlist.rs
index b8ba7e58f2a..88159ce5552 100644
--- a/src/libextra/dlist.rs
+++ b/src/libextra/dlist.rs
@@ -25,7 +25,7 @@
use std::cast;
use std::ptr;
use std::util;
-use std::iterator::{FromIterator, Invert};
+use std::iterator::{FromIterator, Extendable, Invert};
use container::Deque;
@@ -541,11 +541,17 @@ impl DoubleEndedIterator for ConsumeIterator {
impl> FromIterator for DList {
fn from_iterator(iterator: &mut T) -> DList {
let mut ret = DList::new();
- for iterator.advance |elt| { ret.push_back(elt); }
+ ret.extend(iterator);
ret
}
}
+impl> Extendable for DList {
+ fn extend(&mut self, iterator: &mut T) {
+ for iterator.advance |elt| { self.push_back(elt); }
+ }
+}
+
impl Eq for DList {
fn eq(&self, other: &DList) -> bool {
self.len() == other.len() &&
diff --git a/src/libextra/priority_queue.rs b/src/libextra/priority_queue.rs
index dd24a2a9eb9..1c92a4f34e5 100644
--- a/src/libextra/priority_queue.rs
+++ b/src/libextra/priority_queue.rs
@@ -16,7 +16,7 @@ use std::clone::Clone;
use std::unstable::intrinsics::{move_val_init, init};
use std::util::{replace, swap};
use std::vec;
-use std::iterator::FromIterator;
+use std::iterator::{FromIterator, Extendable};
/// A priority queue implemented with a binary heap
#[deriving(Clone)]
@@ -191,20 +191,27 @@ impl<'self, T> Iterator<&'self T> for PriorityQueueIterator<'self, T> {
}
impl> FromIterator for PriorityQueue {
- pub fn from_iterator(iter: &mut Iter) -> PriorityQueue {
- let (lower, _) = iter.size_hint();
-
+ fn from_iterator(iter: &mut Iter) -> PriorityQueue {
let mut q = PriorityQueue::new();
- q.reserve_at_least(lower);
-
- for iter.advance |elem| {
- q.push(elem);
- }
+ q.extend(iter);
q
}
}
+impl> Extendable for PriorityQueue {
+ fn extend(&mut self, iter: &mut Iter) {
+ let (lower, _) = iter.size_hint();
+
+ let len = self.capacity();
+ self.reserve_at_least(len + lower);
+
+ for iter.advance |elem| {
+ self.push(elem);
+ }
+ }
+}
+
#[cfg(test)]
mod tests {
use sort::merge_sort;
diff --git a/src/libextra/ringbuf.rs b/src/libextra/ringbuf.rs
index 90f37cbf526..92183f22d3b 100644
--- a/src/libextra/ringbuf.rs
+++ b/src/libextra/ringbuf.rs
@@ -16,7 +16,7 @@
use std::num;
use std::uint;
use std::vec;
-use std::iterator::{FromIterator, Invert, RandomAccessIterator};
+use std::iterator::{FromIterator, Invert, RandomAccessIterator, Extendable};
use container::Deque;
@@ -325,14 +325,21 @@ impl Eq for RingBuf {
impl> FromIterator for RingBuf {
fn from_iterator(iterator: &mut T) -> RingBuf {
- let mut deq = RingBuf::new();
- for iterator.advance |elt| {
- deq.push_back(elt);
- }
+ let (lower, _) = iterator.size_hint();
+ let mut deq = RingBuf::with_capacity(lower);
+ deq.extend(iterator);
deq
}
}
+impl> Extendable for RingBuf {
+ fn extend(&mut self, iterator: &mut T) {
+ for iterator.advance |elt| {
+ self.push_back(elt);
+ }
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/src/libextra/treemap.rs b/src/libextra/treemap.rs
index 8c7ace56412..6148e14b79f 100644
--- a/src/libextra/treemap.rs
+++ b/src/libextra/treemap.rs
@@ -15,7 +15,7 @@
use std::num;
use std::util::{swap, replace};
-use std::iterator::FromIterator;
+use std::iterator::{FromIterator, Extendable};
// This is implemented as an AA tree, which is a simplified variation of
// a red-black tree where red (horizontal) nodes can only be added
@@ -753,29 +753,39 @@ fn remove(node: &mut Option<~TreeNode>,
}
impl> FromIterator<(K, V), T> for TreeMap {
- pub fn from_iterator(iter: &mut T) -> TreeMap {
+ fn from_iterator(iter: &mut T) -> TreeMap {
let mut map = TreeMap::new();
-
- for iter.advance |(k, v)| {
- map.insert(k, v);
- }
-
+ map.extend(iter);
map
}
}
+impl> Extendable<(K, V), T> for TreeMap {
+ #[inline]
+ fn extend(&mut self, iter: &mut T) {
+ for iter.advance |(k, v)| {
+ self.insert(k, v);
+ }
+ }
+}
+
impl> FromIterator for TreeSet {
pub fn from_iterator(iter: &mut Iter) -> TreeSet {
let mut set = TreeSet::new();
-
- for iter.advance |elem| {
- set.insert(elem);
- }
-
+ set.extend(iter);
set
}
}
+impl> Extendable for TreeSet {
+ #[inline]
+ fn extend(&mut self, iter: &mut Iter) {
+ for iter.advance |elem| {
+ self.insert(elem);
+ }
+ }
+}
+
#[cfg(test)]
mod test_treemap {