@@ -11,6 +11,7 @@ use core::cmp::{self, Ordering};
11
11
use core:: fmt;
12
12
use core:: hash:: { Hash , Hasher } ;
13
13
use core:: iter:: { repeat_n, repeat_with, ByRefSized } ;
14
+ use core:: marker:: PhantomData ;
14
15
use core:: mem:: { ManuallyDrop , SizedTypeProperties } ;
15
16
use core:: ops:: { Index , IndexMut , Range , RangeBounds } ;
16
17
use core:: ptr;
@@ -102,7 +103,8 @@ pub struct VecDeque<
102
103
// if `len == 0`, the exact value of `head` is unimportant.
103
104
// if `T` is zero-Sized, then `self.len <= usize::MAX`, otherwise `self.len <= isize::MAX as usize`.
104
105
len : usize ,
105
- buf : RawVec < T , A > ,
106
+ buf : RawVec < A > ,
107
+ _marker : PhantomData < T > ,
106
108
}
107
109
108
110
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -144,7 +146,7 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for VecDeque<T, A> {
144
146
// use drop for [T]
145
147
ptr:: drop_in_place ( front) ;
146
148
}
147
- // RawVec handles deallocation
149
+ self . buf . drop_if_needed ( T :: LAYOUT ) ;
148
150
}
149
151
}
150
152
@@ -545,7 +547,7 @@ impl<T> VecDeque<T> {
545
547
#[ must_use]
546
548
pub const fn new ( ) -> VecDeque < T > {
547
549
// FIXME: This should just be `VecDeque::new_in(Global)` once that hits stable.
548
- VecDeque { head : 0 , len : 0 , buf : RawVec :: NEW }
550
+ VecDeque { head : 0 , len : 0 , buf : RawVec :: new :: < T > ( ) , _marker : PhantomData }
549
551
}
550
552
551
553
/// Creates an empty deque with space for at least `capacity` elements.
@@ -585,7 +587,12 @@ impl<T> VecDeque<T> {
585
587
#[ inline]
586
588
#[ unstable( feature = "try_with_capacity" , issue = "91913" ) ]
587
589
pub fn try_with_capacity ( capacity : usize ) -> Result < VecDeque < T > , TryReserveError > {
588
- Ok ( VecDeque { head : 0 , len : 0 , buf : RawVec :: try_with_capacity_in ( capacity, Global ) ? } )
590
+ Ok ( VecDeque {
591
+ head : 0 ,
592
+ len : 0 ,
593
+ buf : RawVec :: try_with_capacity_in ( capacity, Global , T :: LAYOUT ) ?,
594
+ _marker : PhantomData ,
595
+ } )
589
596
}
590
597
}
591
598
@@ -602,7 +609,12 @@ impl<T, A: Allocator> VecDeque<T, A> {
602
609
#[ inline]
603
610
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
604
611
pub const fn new_in ( alloc : A ) -> VecDeque < T , A > {
605
- VecDeque { head : 0 , len : 0 , buf : RawVec :: new_in ( alloc) }
612
+ VecDeque {
613
+ head : 0 ,
614
+ len : 0 ,
615
+ buf : RawVec :: new_in ( alloc, core:: mem:: align_of :: < T > ( ) ) ,
616
+ _marker : PhantomData ,
617
+ }
606
618
}
607
619
608
620
/// Creates an empty deque with space for at least `capacity` elements.
@@ -616,7 +628,12 @@ impl<T, A: Allocator> VecDeque<T, A> {
616
628
/// ```
617
629
#[ unstable( feature = "allocator_api" , issue = "32838" ) ]
618
630
pub fn with_capacity_in ( capacity : usize , alloc : A ) -> VecDeque < T , A > {
619
- VecDeque { head : 0 , len : 0 , buf : RawVec :: with_capacity_in ( capacity, alloc) }
631
+ VecDeque {
632
+ head : 0 ,
633
+ len : 0 ,
634
+ buf : RawVec :: with_capacity_in ( capacity, alloc, T :: LAYOUT ) ,
635
+ _marker : PhantomData ,
636
+ }
620
637
}
621
638
622
639
/// Creates a `VecDeque` from a raw allocation, when the initialized
@@ -647,6 +664,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
647
664
head : initialized. start ,
648
665
len : initialized. end . unchecked_sub ( initialized. start ) ,
649
666
buf : RawVec :: from_raw_parts_in ( ptr, capacity, alloc) ,
667
+ _marker : PhantomData ,
650
668
}
651
669
}
652
670
}
@@ -753,7 +771,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
753
771
#[ inline]
754
772
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
755
773
pub fn capacity ( & self ) -> usize {
756
- if T :: IS_ZST { usize :: MAX } else { self . buf . capacity ( ) }
774
+ self . buf . capacity ( core :: mem :: size_of :: < T > ( ) )
757
775
}
758
776
759
777
/// Reserves the minimum capacity for at least `additional` more elements to be inserted in the
@@ -784,7 +802,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
784
802
let old_cap = self . capacity ( ) ;
785
803
786
804
if new_cap > old_cap {
787
- self . buf . reserve_exact ( self . len , additional) ;
805
+ self . buf . reserve_exact ( self . len , additional, T :: LAYOUT ) ;
788
806
unsafe {
789
807
self . handle_capacity_increase ( old_cap) ;
790
808
}
@@ -815,7 +833,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
815
833
if new_cap > old_cap {
816
834
// we don't need to reserve_exact(), as the size doesn't have
817
835
// to be a power of 2.
818
- self . buf . reserve ( self . len , additional) ;
836
+ self . buf . reserve ( self . len , additional, T :: LAYOUT ) ;
819
837
unsafe {
820
838
self . handle_capacity_increase ( old_cap) ;
821
839
}
@@ -866,7 +884,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
866
884
let old_cap = self . capacity ( ) ;
867
885
868
886
if new_cap > old_cap {
869
- self . buf . try_reserve_exact ( self . len , additional) ?;
887
+ self . buf . try_reserve_exact ( self . len , additional, T :: LAYOUT ) ?;
870
888
unsafe {
871
889
self . handle_capacity_increase ( old_cap) ;
872
890
}
@@ -914,7 +932,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
914
932
let old_cap = self . capacity ( ) ;
915
933
916
934
if new_cap > old_cap {
917
- self . buf . try_reserve ( self . len , additional) ?;
935
+ self . buf . try_reserve ( self . len , additional, T :: LAYOUT ) ?;
918
936
unsafe {
919
937
self . handle_capacity_increase ( old_cap) ;
920
938
}
@@ -1056,7 +1074,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
1056
1074
1057
1075
let guard = Guard { deque : self , old_head, target_cap } ;
1058
1076
1059
- guard. deque . buf . shrink_to_fit ( target_cap) ;
1077
+ guard. deque . buf . shrink_to_fit ( target_cap, T :: LAYOUT ) ;
1060
1078
1061
1079
// Don't drop the guard if we didn't unwind.
1062
1080
mem:: forget ( guard) ;
@@ -2161,7 +2179,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
2161
2179
// buffer without it being full emerge
2162
2180
debug_assert ! ( self . is_full( ) ) ;
2163
2181
let old_cap = self . capacity ( ) ;
2164
- self . buf . grow_one ( ) ;
2182
+ self . buf . grow_one ( T :: LAYOUT ) ;
2165
2183
unsafe {
2166
2184
self . handle_capacity_increase ( old_cap) ;
2167
2185
}
@@ -2950,7 +2968,12 @@ impl<T, A: Allocator> From<Vec<T, A>> for VecDeque<T, A> {
2950
2968
#[ inline]
2951
2969
fn from ( other : Vec < T , A > ) -> Self {
2952
2970
let ( ptr, len, cap, alloc) = other. into_raw_parts_with_alloc ( ) ;
2953
- Self { head : 0 , len, buf : unsafe { RawVec :: from_raw_parts_in ( ptr, cap, alloc) } }
2971
+ Self {
2972
+ head : 0 ,
2973
+ len,
2974
+ buf : unsafe { RawVec :: from_raw_parts_in ( ptr, cap, alloc) } ,
2975
+ _marker : PhantomData ,
2976
+ }
2954
2977
}
2955
2978
}
2956
2979
@@ -2990,7 +3013,7 @@ impl<T, A: Allocator> From<VecDeque<T, A>> for Vec<T, A> {
2990
3013
2991
3014
unsafe {
2992
3015
let other = ManuallyDrop :: new ( other) ;
2993
- let buf = other. buf . ptr ( ) ;
3016
+ let buf: * mut T = other. buf . ptr ( ) ;
2994
3017
let len = other. len ( ) ;
2995
3018
let cap = other. capacity ( ) ;
2996
3019
let alloc = ptr:: read ( other. allocator ( ) ) ;
0 commit comments