Skip to content

Commit 389c3ea

Browse files
committed
Go full Monomorphic
1 parent 205249c commit 389c3ea

17 files changed

+343
-392
lines changed

library/alloc/src/boxed.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ impl<T> Box<[T]> {
673673
#[unstable(feature = "new_uninit", issue = "63291")]
674674
#[must_use]
675675
pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
676-
unsafe { RawVec::with_capacity(len).into_box(len) }
676+
unsafe { RawVec::with_capacity(len, T::LAYOUT).into_box(len) }
677677
}
678678

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

704704
/// Constructs a new boxed slice with uninitialized contents. Returns an error if
@@ -725,7 +725,7 @@ impl<T> Box<[T]> {
725725
#[inline]
726726
pub fn try_new_uninit_slice(len: usize) -> Result<Box<[mem::MaybeUninit<T>]>, AllocError> {
727727
let ptr = if T::IS_ZST || len == 0 {
728-
NonNull::dangling()
728+
NonNull::<T>::dangling()
729729
} else {
730730
let layout = match Layout::array::<mem::MaybeUninit<T>>(len) {
731731
Ok(l) => l,
@@ -759,7 +759,7 @@ impl<T> Box<[T]> {
759759
#[inline]
760760
pub fn try_new_zeroed_slice(len: usize) -> Result<Box<[mem::MaybeUninit<T>]>, AllocError> {
761761
let ptr = if T::IS_ZST || len == 0 {
762-
NonNull::dangling()
762+
NonNull::<T>::dangling()
763763
} else {
764764
let layout = match Layout::array::<mem::MaybeUninit<T>>(len) {
765765
Ok(l) => l,
@@ -799,7 +799,7 @@ impl<T, A: Allocator> Box<[T], A> {
799799
// #[unstable(feature = "new_uninit", issue = "63291")]
800800
#[must_use]
801801
pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
802-
unsafe { RawVec::with_capacity_in(len, alloc).into_box(len) }
802+
unsafe { RawVec::with_capacity_in(len, alloc, T::LAYOUT).into_box(len) }
803803
}
804804

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

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

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

+38-15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ 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;
1415
use core::mem::{ManuallyDrop, SizedTypeProperties};
1516
use core::ops::{Index, IndexMut, Range, RangeBounds};
1617
use core::ptr;
@@ -102,7 +103,8 @@ pub struct VecDeque<
102103
// if `len == 0`, the exact value of `head` is unimportant.
103104
// if `T` is zero-Sized, then `self.len <= usize::MAX`, otherwise `self.len <= isize::MAX as usize`.
104105
len: usize,
105-
buf: RawVec<T, A>,
106+
buf: RawVec<A>,
107+
_marker: PhantomData<T>,
106108
}
107109

108110
#[stable(feature = "rust1", since = "1.0.0")]
@@ -144,7 +146,7 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for VecDeque<T, A> {
144146
// use drop for [T]
145147
ptr::drop_in_place(front);
146148
}
147-
// RawVec handles deallocation
149+
self.buf.drop_if_needed(T::LAYOUT);
148150
}
149151
}
150152

@@ -545,7 +547,7 @@ impl<T> VecDeque<T> {
545547
#[must_use]
546548
pub const fn new() -> VecDeque<T> {
547549
// 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 }
549551
}
550552

551553
/// Creates an empty deque with space for at least `capacity` elements.
@@ -585,7 +587,12 @@ impl<T> VecDeque<T> {
585587
#[inline]
586588
#[unstable(feature = "try_with_capacity", issue = "91913")]
587589
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+
})
589596
}
590597
}
591598

@@ -602,7 +609,12 @@ impl<T, A: Allocator> VecDeque<T, A> {
602609
#[inline]
603610
#[unstable(feature = "allocator_api", issue = "32838")]
604611
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+
}
606618
}
607619

608620
/// Creates an empty deque with space for at least `capacity` elements.
@@ -616,7 +628,12 @@ impl<T, A: Allocator> VecDeque<T, A> {
616628
/// ```
617629
#[unstable(feature = "allocator_api", issue = "32838")]
618630
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+
}
620637
}
621638

622639
/// Creates a `VecDeque` from a raw allocation, when the initialized
@@ -647,6 +664,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
647664
head: initialized.start,
648665
len: initialized.end.unchecked_sub(initialized.start),
649666
buf: RawVec::from_raw_parts_in(ptr, capacity, alloc),
667+
_marker: PhantomData,
650668
}
651669
}
652670
}
@@ -753,7 +771,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
753771
#[inline]
754772
#[stable(feature = "rust1", since = "1.0.0")]
755773
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>())
757775
}
758776

759777
/// 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> {
784802
let old_cap = self.capacity();
785803

786804
if new_cap > old_cap {
787-
self.buf.reserve_exact(self.len, additional);
805+
self.buf.reserve_exact(self.len, additional, T::LAYOUT);
788806
unsafe {
789807
self.handle_capacity_increase(old_cap);
790808
}
@@ -815,7 +833,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
815833
if new_cap > old_cap {
816834
// we don't need to reserve_exact(), as the size doesn't have
817835
// to be a power of 2.
818-
self.buf.reserve(self.len, additional);
836+
self.buf.reserve(self.len, additional, T::LAYOUT);
819837
unsafe {
820838
self.handle_capacity_increase(old_cap);
821839
}
@@ -866,7 +884,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
866884
let old_cap = self.capacity();
867885

868886
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)?;
870888
unsafe {
871889
self.handle_capacity_increase(old_cap);
872890
}
@@ -914,7 +932,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
914932
let old_cap = self.capacity();
915933

916934
if new_cap > old_cap {
917-
self.buf.try_reserve(self.len, additional)?;
935+
self.buf.try_reserve(self.len, additional, T::LAYOUT)?;
918936
unsafe {
919937
self.handle_capacity_increase(old_cap);
920938
}
@@ -1056,7 +1074,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
10561074

10571075
let guard = Guard { deque: self, old_head, target_cap };
10581076

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

10611079
// Don't drop the guard if we didn't unwind.
10621080
mem::forget(guard);
@@ -2161,7 +2179,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
21612179
// buffer without it being full emerge
21622180
debug_assert!(self.is_full());
21632181
let old_cap = self.capacity();
2164-
self.buf.grow_one();
2182+
self.buf.grow_one(T::LAYOUT);
21652183
unsafe {
21662184
self.handle_capacity_increase(old_cap);
21672185
}
@@ -2950,7 +2968,12 @@ impl<T, A: Allocator> From<Vec<T, A>> for VecDeque<T, A> {
29502968
#[inline]
29512969
fn from(other: Vec<T, A>) -> Self {
29522970
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+
}
29542977
}
29552978
}
29562979

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

29913014
unsafe {
29923015
let other = ManuallyDrop::new(other);
2993-
let buf = other.buf.ptr();
3016+
let buf: *mut T = other.buf.ptr();
29943017
let len = other.len();
29953018
let cap = other.capacity();
29963019
let alloc = ptr::read(other.allocator());

0 commit comments

Comments
 (0)