Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7c7067f

Browse files
committed
Move RawVec logic to RawVecInner
1 parent 6c65e87 commit 7c7067f

17 files changed

+487
-420
lines changed

library/alloc/src/boxed.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ impl<T> Box<[T]> {
675675
#[unstable(feature = "new_uninit", issue = "63291")]
676676
#[must_use]
677677
pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
678-
unsafe { RawVec::with_capacity(len, T::LAYOUT).into_box(len) }
678+
unsafe { RawVec::with_capacity(len).into_box(len) }
679679
}
680680

681681
/// Constructs a new boxed slice with uninitialized contents, with the memory
@@ -700,7 +700,7 @@ impl<T> Box<[T]> {
700700
#[unstable(feature = "new_uninit", issue = "63291")]
701701
#[must_use]
702702
pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
703-
unsafe { RawVec::with_capacity_zeroed(len, T::LAYOUT).into_box(len) }
703+
unsafe { RawVec::with_capacity_zeroed(len).into_box(len) }
704704
}
705705

706706
/// Constructs a new boxed slice with uninitialized contents. Returns an error if
@@ -727,7 +727,7 @@ impl<T> Box<[T]> {
727727
#[inline]
728728
pub fn try_new_uninit_slice(len: usize) -> Result<Box<[mem::MaybeUninit<T>]>, AllocError> {
729729
let ptr = if T::IS_ZST || len == 0 {
730-
NonNull::<T>::dangling()
730+
NonNull::dangling()
731731
} else {
732732
let layout = match Layout::array::<mem::MaybeUninit<T>>(len) {
733733
Ok(l) => l,
@@ -761,7 +761,7 @@ impl<T> Box<[T]> {
761761
#[inline]
762762
pub fn try_new_zeroed_slice(len: usize) -> Result<Box<[mem::MaybeUninit<T>]>, AllocError> {
763763
let ptr = if T::IS_ZST || len == 0 {
764-
NonNull::<T>::dangling()
764+
NonNull::dangling()
765765
} else {
766766
let layout = match Layout::array::<mem::MaybeUninit<T>>(len) {
767767
Ok(l) => l,
@@ -801,7 +801,7 @@ impl<T, A: Allocator> Box<[T], A> {
801801
// #[unstable(feature = "new_uninit", issue = "63291")]
802802
#[must_use]
803803
pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
804-
unsafe { RawVec::with_capacity_in(len, alloc, T::LAYOUT).into_box(len) }
804+
unsafe { RawVec::with_capacity_in(len, alloc).into_box(len) }
805805
}
806806

807807
/// Constructs a new boxed slice with uninitialized contents in the provided allocator,
@@ -829,7 +829,7 @@ impl<T, A: Allocator> Box<[T], A> {
829829
// #[unstable(feature = "new_uninit", issue = "63291")]
830830
#[must_use]
831831
pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
832-
unsafe { RawVec::with_capacity_zeroed_in(len, alloc, T::LAYOUT).into_box(len) }
832+
unsafe { RawVec::with_capacity_zeroed_in(len, alloc).into_box(len) }
833833
}
834834
}
835835

@@ -1550,7 +1550,7 @@ impl<T: Copy> BoxFromSlice<T> for Box<[T]> {
15501550
#[inline]
15511551
fn from_slice(slice: &[T]) -> Self {
15521552
let len = slice.len();
1553-
let buf = RawVec::with_capacity(len, T::LAYOUT);
1553+
let buf = RawVec::with_capacity(len);
15541554
unsafe {
15551555
ptr::copy_nonoverlapping(slice.as_ptr(), buf.ptr(), len);
15561556
buf.into_box(slice.len()).assume_init()

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

Lines changed: 17 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use core::cmp::{self, Ordering};
1111
use core::fmt;
1212
use core::hash::{Hash, Hasher};
1313
use core::iter::{repeat_n, repeat_with, ByRefSized};
14-
use core::marker::PhantomData;
1514
use core::mem::{ManuallyDrop, SizedTypeProperties};
1615
use core::ops::{Index, IndexMut, Range, RangeBounds};
1716
use core::ptr;
@@ -103,8 +102,7 @@ pub struct VecDeque<
103102
// if `len == 0`, the exact value of `head` is unimportant.
104103
// if `T` is zero-Sized, then `self.len <= usize::MAX`, otherwise `self.len <= isize::MAX as usize`.
105104
len: usize,
106-
buf: RawVec<A>,
107-
_marker: PhantomData<T>,
105+
buf: RawVec<T, A>,
108106
}
109107

110108
#[stable(feature = "rust1", since = "1.0.0")]
@@ -128,24 +126,11 @@ impl<T: Clone, A: Allocator + Clone> Clone for VecDeque<T, A> {
128126
#[stable(feature = "rust1", since = "1.0.0")]
129127
unsafe impl<#[may_dangle] T, A: Allocator> Drop for VecDeque<T, A> {
130128
fn drop(&mut self) {
131-
use core::alloc::Layout;
132-
133-
struct Guard<'a, A: Allocator> {
134-
buf: &'a mut RawVec<A>,
135-
layout: Layout,
136-
}
137-
138-
impl<A: Allocator> Drop for Guard<'_, A> {
139-
fn drop(&mut self) {
140-
self.buf.drop(self.layout);
141-
}
142-
}
143-
144129
/// Runs the destructor for all items in the slice when it gets dropped (normally or
145130
/// during unwinding).
146-
struct Dropper<T>(*mut [T]);
131+
struct Dropper<'a, T>(&'a mut [T]);
147132

148-
impl<T> Drop for Dropper<T> {
133+
impl<'a, T> Drop for Dropper<'a, T> {
149134
fn drop(&mut self) {
150135
unsafe {
151136
ptr::drop_in_place(self.0);
@@ -154,16 +139,12 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for VecDeque<T, A> {
154139
}
155140

156141
let (front, back) = self.as_mut_slices();
157-
let front = front as *mut [T];
158-
let back = back as *mut [T];
159-
160-
let _guard = Guard { buf: &mut self.buf, layout: T::LAYOUT };
161-
162142
unsafe {
163143
let _back_dropper = Dropper(back);
164144
// use drop for [T]
165145
ptr::drop_in_place(front);
166146
}
147+
// RawVec handles deallocation
167148
}
168149
}
169150

@@ -564,7 +545,7 @@ impl<T> VecDeque<T> {
564545
#[must_use]
565546
pub const fn new() -> VecDeque<T> {
566547
// FIXME: This should just be `VecDeque::new_in(Global)` once that hits stable.
567-
VecDeque { head: 0, len: 0, buf: RawVec::new::<T>(), _marker: PhantomData }
548+
VecDeque { head: 0, len: 0, buf: RawVec::NEW }
568549
}
569550

570551
/// Creates an empty deque with space for at least `capacity` elements.
@@ -604,12 +585,7 @@ impl<T> VecDeque<T> {
604585
#[inline]
605586
#[unstable(feature = "try_with_capacity", issue = "91913")]
606587
pub fn try_with_capacity(capacity: usize) -> Result<VecDeque<T>, TryReserveError> {
607-
Ok(VecDeque {
608-
head: 0,
609-
len: 0,
610-
buf: RawVec::try_with_capacity_in(capacity, Global, T::LAYOUT)?,
611-
_marker: PhantomData,
612-
})
588+
Ok(VecDeque { head: 0, len: 0, buf: RawVec::try_with_capacity_in(capacity, Global)? })
613589
}
614590
}
615591

@@ -626,12 +602,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
626602
#[inline]
627603
#[unstable(feature = "allocator_api", issue = "32838")]
628604
pub const fn new_in(alloc: A) -> VecDeque<T, A> {
629-
VecDeque {
630-
head: 0,
631-
len: 0,
632-
buf: RawVec::new_in(alloc, core::mem::align_of::<T>()),
633-
_marker: PhantomData,
634-
}
605+
VecDeque { head: 0, len: 0, buf: RawVec::new_in(alloc) }
635606
}
636607

637608
/// Creates an empty deque with space for at least `capacity` elements.
@@ -645,12 +616,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
645616
/// ```
646617
#[unstable(feature = "allocator_api", issue = "32838")]
647618
pub fn with_capacity_in(capacity: usize, alloc: A) -> VecDeque<T, A> {
648-
VecDeque {
649-
head: 0,
650-
len: 0,
651-
buf: RawVec::with_capacity_in(capacity, alloc, T::LAYOUT),
652-
_marker: PhantomData,
653-
}
619+
VecDeque { head: 0, len: 0, buf: RawVec::with_capacity_in(capacity, alloc) }
654620
}
655621

656622
/// Creates a `VecDeque` from a raw allocation, when the initialized
@@ -681,7 +647,6 @@ impl<T, A: Allocator> VecDeque<T, A> {
681647
head: initialized.start,
682648
len: initialized.end.unchecked_sub(initialized.start),
683649
buf: RawVec::from_raw_parts_in(ptr, capacity, alloc),
684-
_marker: PhantomData,
685650
}
686651
}
687652
}
@@ -788,7 +753,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
788753
#[inline]
789754
#[stable(feature = "rust1", since = "1.0.0")]
790755
pub fn capacity(&self) -> usize {
791-
self.buf.capacity(core::mem::size_of::<T>())
756+
if T::IS_ZST { usize::MAX } else { self.buf.capacity() }
792757
}
793758

794759
/// Reserves the minimum capacity for at least `additional` more elements to be inserted in the
@@ -819,7 +784,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
819784
let old_cap = self.capacity();
820785

821786
if new_cap > old_cap {
822-
self.buf.reserve_exact(self.len, additional, T::LAYOUT);
787+
self.buf.reserve_exact(self.len, additional);
823788
unsafe {
824789
self.handle_capacity_increase(old_cap);
825790
}
@@ -850,7 +815,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
850815
if new_cap > old_cap {
851816
// we don't need to reserve_exact(), as the size doesn't have
852817
// to be a power of 2.
853-
self.buf.reserve(self.len, additional, T::LAYOUT);
818+
self.buf.reserve(self.len, additional);
854819
unsafe {
855820
self.handle_capacity_increase(old_cap);
856821
}
@@ -901,7 +866,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
901866
let old_cap = self.capacity();
902867

903868
if new_cap > old_cap {
904-
self.buf.try_reserve_exact(self.len, additional, T::LAYOUT)?;
869+
self.buf.try_reserve_exact(self.len, additional)?;
905870
unsafe {
906871
self.handle_capacity_increase(old_cap);
907872
}
@@ -949,7 +914,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
949914
let old_cap = self.capacity();
950915

951916
if new_cap > old_cap {
952-
self.buf.try_reserve(self.len, additional, T::LAYOUT)?;
917+
self.buf.try_reserve(self.len, additional)?;
953918
unsafe {
954919
self.handle_capacity_increase(old_cap);
955920
}
@@ -1091,7 +1056,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
10911056

10921057
let guard = Guard { deque: self, old_head, target_cap };
10931058

1094-
guard.deque.buf.shrink_to_fit(target_cap, T::LAYOUT);
1059+
guard.deque.buf.shrink_to_fit(target_cap);
10951060

10961061
// Don't drop the guard if we didn't unwind.
10971062
mem::forget(guard);
@@ -2196,7 +2161,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
21962161
// buffer without it being full emerge
21972162
debug_assert!(self.is_full());
21982163
let old_cap = self.capacity();
2199-
self.buf.grow_one(T::LAYOUT);
2164+
self.buf.grow_one();
22002165
unsafe {
22012166
self.handle_capacity_increase(old_cap);
22022167
}
@@ -2985,12 +2950,7 @@ impl<T, A: Allocator> From<Vec<T, A>> for VecDeque<T, A> {
29852950
#[inline]
29862951
fn from(other: Vec<T, A>) -> Self {
29872952
let (ptr, len, cap, alloc) = other.into_raw_parts_with_alloc();
2988-
Self {
2989-
head: 0,
2990-
len,
2991-
buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) },
2992-
_marker: PhantomData,
2993-
}
2953+
Self { head: 0, len, buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) } }
29942954
}
29952955
}
29962956

@@ -3030,7 +2990,7 @@ impl<T, A: Allocator> From<VecDeque<T, A>> for Vec<T, A> {
30302990

30312991
unsafe {
30322992
let other = ManuallyDrop::new(other);
3033-
let buf: *mut T = other.buf.ptr();
2993+
let buf = other.buf.ptr();
30342994
let len = other.len();
30352995
let cap = other.capacity();
30362996
let alloc = ptr::read(other.allocator());

0 commit comments

Comments
 (0)