From 0730a01c5ca3b0e8760d72a05c47d4199bd64728 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 5 Apr 2019 14:51:07 -0700 Subject: [PATCH] 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. --- src/liballoc/collections/binary_heap.rs | 4 +--- src/liballoc/collections/btree/map.rs | 4 ++-- src/liballoc/collections/btree/set.rs | 4 ++-- src/liballoc/collections/linked_list.rs | 4 +--- src/liballoc/collections/vec_deque.rs | 4 +--- src/libproc_macro/lib.rs | 4 +--- src/libstd/path.rs | 4 +--- src/libstd/sys_common/wtf8.rs | 4 +--- 8 files changed, 10 insertions(+), 22 deletions(-) diff --git a/src/liballoc/collections/binary_heap.rs b/src/liballoc/collections/binary_heap.rs index a171f128c24d6..8c142a3d317c6 100644 --- a/src/liballoc/collections/binary_heap.rs +++ b/src/liballoc/collections/binary_heap.rs @@ -1177,9 +1177,7 @@ impl BinaryHeap { self.reserve(lower); - for elem in iterator { - self.push(elem); - } + iterator.for_each(move |elem| self.push(elem)); } } diff --git a/src/liballoc/collections/btree/map.rs b/src/liballoc/collections/btree/map.rs index ce29978856ffd..6b079fc87cc78 100644 --- a/src/liballoc/collections/btree/map.rs +++ b/src/liballoc/collections/btree/map.rs @@ -1727,9 +1727,9 @@ impl FromIterator<(K, V)> for BTreeMap { impl Extend<(K, V)> for BTreeMap { #[inline] fn extend>(&mut self, iter: T) { - for (k, v) in iter { + iter.into_iter().for_each(move |(k, v)| { self.insert(k, v); - } + }); } } diff --git a/src/liballoc/collections/btree/set.rs b/src/liballoc/collections/btree/set.rs index e71767077ca55..16a96ca19b824 100644 --- a/src/liballoc/collections/btree/set.rs +++ b/src/liballoc/collections/btree/set.rs @@ -883,9 +883,9 @@ impl<'a, T> IntoIterator for &'a BTreeSet { impl Extend for BTreeSet { #[inline] fn extend>(&mut self, iter: Iter) { - for elem in iter { + iter.into_iter().for_each(move |elem| { self.insert(elem); - } + }); } } diff --git a/src/liballoc/collections/linked_list.rs b/src/liballoc/collections/linked_list.rs index c2ee2e63156cf..d6d84a4f083d0 100644 --- a/src/liballoc/collections/linked_list.rs +++ b/src/liballoc/collections/linked_list.rs @@ -1107,9 +1107,7 @@ impl Extend for LinkedList { impl SpecExtend for LinkedList { 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)); } } diff --git a/src/liballoc/collections/vec_deque.rs b/src/liballoc/collections/vec_deque.rs index 4e90f783ec6a5..4bea615ab861f 100644 --- a/src/liballoc/collections/vec_deque.rs +++ b/src/liballoc/collections/vec_deque.rs @@ -2677,9 +2677,7 @@ impl<'a, T> IntoIterator for &'a mut VecDeque { #[stable(feature = "rust1", since = "1.0.0")] impl Extend for VecDeque { fn extend>(&mut self, iter: T) { - for elt in iter { - self.push_back(elt); - } + iter.into_iter().for_each(move |elt| self.push_back(elt)); } } diff --git a/src/libproc_macro/lib.rs b/src/libproc_macro/lib.rs index 03905f3e705d3..1e0f1ed578aae 100644 --- a/src/libproc_macro/lib.rs +++ b/src/libproc_macro/lib.rs @@ -160,9 +160,7 @@ impl iter::FromIterator for TokenStream { impl iter::FromIterator for TokenStream { fn from_iter>(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()) } } diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 71e82f0a9b02e..1bbda9b5bcb1a 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1551,9 +1551,7 @@ impl> iter::FromIterator

for PathBuf { #[stable(feature = "rust1", since = "1.0.0")] impl> iter::Extend

for PathBuf { fn extend>(&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())); } } diff --git a/src/libstd/sys_common/wtf8.rs b/src/libstd/sys_common/wtf8.rs index 7fe1818291e5d..f17020de44ec5 100644 --- a/src/libstd/sys_common/wtf8.rs +++ b/src/libstd/sys_common/wtf8.rs @@ -388,9 +388,7 @@ impl Extend 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)); } }