Skip to content

Commit a964a37

Browse files
committed
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.
1 parent 1ef685e commit a964a37

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
@@ -2700,12 +2700,18 @@ impl<T, A: Allocator> IndexMut<usize> for VecDeque<T, A> {
27002700

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

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

0 commit comments

Comments
 (0)