Skip to content

Commit 203c876

Browse files
committed
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.
2 parents e72ea1d + a964a37 commit 203c876

File tree

1 file changed

+12
-5
lines changed
  • library/alloc/src/collections/vec_deque

1 file changed

+12
-5
lines changed

library/alloc/src/collections/vec_deque/mod.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -2699,12 +2699,18 @@ impl<T, A: Allocator> IndexMut<usize> for VecDeque<T, A> {
26992699

27002700
#[stable(feature = "rust1", since = "1.0.0")]
27012701
impl<T> FromIterator<T> for VecDeque<T> {
2702+
#[inline]
27022703
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T> {
2703-
let iterator = iter.into_iter();
2704-
let (lower, _) = iterator.size_hint();
2705-
let mut deq = VecDeque::with_capacity(lower);
2706-
deq.extend(iterator);
2707-
deq
2704+
// Since converting is O(1) now, might as well re-use that logic
2705+
// (including things like the `vec::IntoIter`→`Vec` specialization)
2706+
// especially as that could save us some monomorphiziation work
2707+
// if one uses the same iterators (like slice ones) with both.
2708+
return from_iter_via_vec(iter.into_iter());
2709+
2710+
#[inline]
2711+
fn from_iter_via_vec<U>(iter: impl Iterator<Item = U>) -> VecDeque<U> {
2712+
Vec::from_iter(iter).into()
2713+
}
27082714
}
27092715
}
27102716

@@ -2791,6 +2797,7 @@ impl<T, A: Allocator> From<Vec<T, A>> for VecDeque<T, A> {
27912797
/// In its current implementation, this is a very cheap
27922798
/// conversion. This isn't yet a guarantee though, and
27932799
/// shouldn't be relied on.
2800+
#[inline]
27942801
fn from(other: Vec<T, A>) -> Self {
27952802
let (ptr, len, cap, alloc) = other.into_raw_parts_with_alloc();
27962803
Self { head: 0, len, buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) } }

0 commit comments

Comments
 (0)