Use for_each to extend collections
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.
This commit is contained in:
parent
acd8dd6a50
commit
0730a01c5c
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
@ -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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user