about summary refs log tree commit diff
path: root/library/alloc/src/collections
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-12-05 08:45:03 +0000
committerbors <bors@rust-lang.org>2022-12-05 08:45:03 +0000
commit203c8765ea33c65d888febe0e8219c4bb11b0d89 (patch)
treee26611529f67ed3193e9126d85cb651a3395b964 /library/alloc/src/collections
parente72ea1dc376e9c302c68faefafd967a8a7ef255a (diff)
parenta964a37211691ae9a28b76b6002ff55a707e9a8b (diff)
downloadrust-203c8765ea33c65d888febe0e8219c4bb11b0d89.tar.gz
rust-203c8765ea33c65d888febe0e8219c4bb11b0d89.zip
Auto merge of #105046 - scottmcm:vecdeque-vs-vec, r=Mark-Simulacrum
Send `VecDeque::from_iter` via `Vec::from_iter`

Since it's O(1) to convert between them now, might as well reuse the logic.

Mostly for the various specializations it does, but might also save some monomorphization work if, say, people collect slice iterators into both `Vec`s and `VecDeque`s.
Diffstat (limited to 'library/alloc/src/collections')
-rw-r--r--library/alloc/src/collections/vec_deque/mod.rs17
1 files changed, 12 insertions, 5 deletions
diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs
index ee8032ad6f0..4866c53e7d5 100644
--- a/library/alloc/src/collections/vec_deque/mod.rs
+++ b/library/alloc/src/collections/vec_deque/mod.rs
@@ -2699,12 +2699,18 @@ impl<T, A: Allocator> IndexMut<usize> for VecDeque<T, A> {
 
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> FromIterator<T> for VecDeque<T> {
+    #[inline]
     fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T> {
-        let iterator = iter.into_iter();
-        let (lower, _) = iterator.size_hint();
-        let mut deq = VecDeque::with_capacity(lower);
-        deq.extend(iterator);
-        deq
+        // Since converting is O(1) now, might as well re-use that logic
+        // (including things like the `vec::IntoIter`→`Vec` specialization)
+        // especially as that could save us some monomorphiziation work
+        // if one uses the same iterators (like slice ones) with both.
+        return from_iter_via_vec(iter.into_iter());
+
+        #[inline]
+        fn from_iter_via_vec<U>(iter: impl Iterator<Item = U>) -> VecDeque<U> {
+            Vec::from_iter(iter).into()
+        }
     }
 }
 
@@ -2791,6 +2797,7 @@ impl<T, A: Allocator> From<Vec<T, A>> for VecDeque<T, A> {
     /// In its current implementation, this is a very cheap
     /// conversion. This isn't yet a guarantee though, and
     /// shouldn't be relied on.
+    #[inline]
     fn from(other: Vec<T, A>) -> Self {
         let (ptr, len, cap, alloc) = other.into_raw_parts_with_alloc();
         Self { head: 0, len, buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) } }