Skip to content

Commit

Permalink
Use for_each to extend collections
Browse files Browse the repository at this point in the history
This updates the `Extend` implementations to use `for_each` for many
collections: `BinaryHeap`, `BTreeMap`, `BTreeSet`, `LinkedList`, `Path`,
`TokenStream`, `VecDeque`, and `Wtf8Buf`.

Folding with `for_each` enables better performance than a `for`-loop for
some iterators, especially if they can just forward to internal
iterators, like `Chain` and `FlatMap` do.
  • Loading branch information
cuviper committed Apr 5, 2019
1 parent acd8dd6 commit 0730a01
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 22 deletions.
4 changes: 1 addition & 3 deletions src/liballoc/collections/binary_heap.rs
Expand Up @@ -1177,9 +1177,7 @@ impl<T: Ord> BinaryHeap<T> {

self.reserve(lower);

for elem in iterator {
self.push(elem);
}
iterator.for_each(move |elem| self.push(elem));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/liballoc/collections/btree/map.rs
Expand Up @@ -1727,9 +1727,9 @@ impl<K: Ord, V> FromIterator<(K, V)> for BTreeMap<K, V> {
impl<K: Ord, V> Extend<(K, V)> for BTreeMap<K, V> {
#[inline]
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
for (k, v) in iter {
iter.into_iter().for_each(move |(k, v)| {
self.insert(k, v);
}
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/liballoc/collections/btree/set.rs
Expand Up @@ -883,9 +883,9 @@ impl<'a, T> IntoIterator for &'a BTreeSet<T> {
impl<T: Ord> Extend<T> for BTreeSet<T> {
#[inline]
fn extend<Iter: IntoIterator<Item = T>>(&mut self, iter: Iter) {
for elem in iter {
iter.into_iter().for_each(move |elem| {
self.insert(elem);
}
});
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/liballoc/collections/linked_list.rs
Expand Up @@ -1107,9 +1107,7 @@ impl<T> Extend<T> for LinkedList<T> {

impl<I: IntoIterator> SpecExtend<I> for LinkedList<I::Item> {
default fn spec_extend(&mut self, iter: I) {
for elt in iter {
self.push_back(elt);
}
iter.into_iter().for_each(move |elt| self.push_back(elt));
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/liballoc/collections/vec_deque.rs
Expand Up @@ -2677,9 +2677,7 @@ impl<'a, T> IntoIterator for &'a mut VecDeque<T> {
#[stable(feature = "rust1", since = "1.0.0")]
impl<A> Extend<A> for VecDeque<A> {
fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T) {
for elt in iter {
self.push_back(elt);
}
iter.into_iter().for_each(move |elt| self.push_back(elt));
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/libproc_macro/lib.rs
Expand Up @@ -160,9 +160,7 @@ impl iter::FromIterator<TokenTree> for TokenStream {
impl iter::FromIterator<TokenStream> for TokenStream {
fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self {
let mut builder = bridge::client::TokenStreamBuilder::new();
for stream in streams {
builder.push(stream.0);
}
streams.into_iter().for_each(|stream| builder.push(stream.0));
TokenStream(builder.build())
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/libstd/path.rs
Expand Up @@ -1551,9 +1551,7 @@ impl<P: AsRef<Path>> iter::FromIterator<P> for PathBuf {
#[stable(feature = "rust1", since = "1.0.0")]
impl<P: AsRef<Path>> iter::Extend<P> for PathBuf {
fn extend<I: IntoIterator<Item = P>>(&mut self, iter: I) {
for p in iter {
self.push(p.as_ref())
}
iter.into_iter().for_each(move |p| self.push(p.as_ref()));
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/libstd/sys_common/wtf8.rs
Expand Up @@ -388,9 +388,7 @@ impl Extend<CodePoint> for Wtf8Buf {
let (low, _high) = iterator.size_hint();
// Lower bound of one byte per code point (ASCII only)
self.bytes.reserve(low);
for code_point in iterator {
self.push(code_point);
}
iterator.for_each(move |code_point| self.push(code_point));
}
}

Expand Down

0 comments on commit 0730a01

Please sign in to comment.