@@ -55,6 +55,10 @@ use self::spec_extend::SpecExtend;
55
55
56
56
mod spec_extend;
57
57
58
+ use self :: spec_from_iter:: SpecFromIter ;
59
+
60
+ mod spec_from_iter;
61
+
58
62
#[ cfg( test) ]
59
63
mod tests;
60
64
@@ -586,6 +590,35 @@ impl<T, A: Allocator> VecDeque<T, A> {
586
590
VecDeque { head : 0 , len : 0 , buf : RawVec :: with_capacity_in ( capacity, alloc) }
587
591
}
588
592
593
+ /// For use by `vec::IntoIter::into_vecdeque`
594
+ ///
595
+ /// # Safety
596
+ ///
597
+ /// All the usual requirements on the allocated memory like in
598
+ /// `Vec::from_raw_parts_in`, but takes a *range* of elements that are
599
+ /// initialized rather than only supporting `0..len`. Requires that
600
+ /// `initialized.start` ≤ `initialized.end` ≤ `capacity`.
601
+ #[ inline]
602
+ pub ( crate ) unsafe fn from_contiguous_raw_parts_in (
603
+ ptr : * mut T ,
604
+ initialized : Range < usize > ,
605
+ capacity : usize ,
606
+ alloc : A ,
607
+ ) -> Self {
608
+ debug_assert ! ( initialized. start <= initialized. end) ;
609
+ debug_assert ! ( initialized. end <= capacity) ;
610
+
611
+ // SAFETY: Our safety precondition guarantees the range length won't wrap,
612
+ // and that the allocation is valid for use in `RawVec`.
613
+ unsafe {
614
+ VecDeque {
615
+ head : initialized. start ,
616
+ len : initialized. end . unchecked_sub ( initialized. start ) ,
617
+ buf : RawVec :: from_raw_parts_in ( ptr, capacity, alloc) ,
618
+ }
619
+ }
620
+ }
621
+
589
622
/// Provides a reference to the element at the given index.
590
623
///
591
624
/// Element at index 0 is the front of the queue.
@@ -2699,18 +2732,8 @@ impl<T, A: Allocator> IndexMut<usize> for VecDeque<T, A> {
2699
2732
2700
2733
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2701
2734
impl < T > FromIterator < T > for VecDeque < T > {
2702
- #[ inline]
2703
2735
fn from_iter < I : IntoIterator < Item = T > > ( iter : I ) -> VecDeque < T > {
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
- }
2736
+ SpecFromIter :: spec_from_iter ( iter. into_iter ( ) )
2714
2737
}
2715
2738
}
2716
2739
0 commit comments