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

Commit f16d415

Browse files
committed
Auto merge of rust-lang#126793 - saethlin:mono-rawvec, r=<try>
Apply "polymorphization at home" to RawVec The idea here is to move all the logic in RawVec into functions with explicit size and alignment parameters. This should eliminate all the fussing about how tweaking RawVec code produces large swings in compile times. This uncovered rust-lang/rust-clippy#12979, so I've modified the relevant test in a way that tries to preserve the spirit of the test without tripping the ICE.
2 parents 00167ab + ccc5c50 commit f16d415

File tree

15 files changed

+277
-218
lines changed

15 files changed

+277
-218
lines changed

library/alloc/src/boxed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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::dangling()
730+
NonNull::<T>::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::dangling()
764+
NonNull::<T>::dangling()
765765
} else {
766766
let layout = match Layout::array::<mem::MaybeUninit<T>>(len) {
767767
Ok(l) => l,

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

Lines changed: 31 additions & 7 deletions
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;
@@ -103,6 +104,7 @@ pub struct VecDeque<
103104
// if `T` is zero-Sized, then `self.len <= usize::MAX`, otherwise `self.len <= isize::MAX as usize`.
104105
len: usize,
105106
buf: RawVec<T, A>,
107+
_marker: PhantomData<T>,
106108
}
107109

108110
#[stable(feature = "rust1", since = "1.0.0")]
@@ -139,6 +141,7 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for VecDeque<T, A> {
139141
}
140142

141143
let (front, back) = self.as_mut_slices();
144+
142145
unsafe {
143146
let _back_dropper = Dropper(back);
144147
// use drop for [T]
@@ -559,7 +562,7 @@ impl<T> VecDeque<T> {
559562
#[must_use]
560563
pub const fn new() -> VecDeque<T> {
561564
// FIXME: This should just be `VecDeque::new_in(Global)` once that hits stable.
562-
VecDeque { head: 0, len: 0, buf: RawVec::NEW }
565+
VecDeque { head: 0, len: 0, buf: RawVec::new(), _marker: PhantomData }
563566
}
564567

565568
/// Creates an empty deque with space for at least `capacity` elements.
@@ -599,7 +602,12 @@ impl<T> VecDeque<T> {
599602
#[inline]
600603
#[unstable(feature = "try_with_capacity", issue = "91913")]
601604
pub fn try_with_capacity(capacity: usize) -> Result<VecDeque<T>, TryReserveError> {
602-
Ok(VecDeque { head: 0, len: 0, buf: RawVec::try_with_capacity_in(capacity, Global)? })
605+
Ok(VecDeque {
606+
head: 0,
607+
len: 0,
608+
buf: RawVec::try_with_capacity_in(capacity, Global)?,
609+
_marker: PhantomData,
610+
})
603611
}
604612
}
605613

@@ -616,7 +624,12 @@ impl<T, A: Allocator> VecDeque<T, A> {
616624
#[inline]
617625
#[unstable(feature = "allocator_api", issue = "32838")]
618626
pub const fn new_in(alloc: A) -> VecDeque<T, A> {
619-
VecDeque { head: 0, len: 0, buf: RawVec::new_in(alloc) }
627+
VecDeque {
628+
head: 0,
629+
len: 0,
630+
buf: RawVec::new_in(alloc, core::mem::align_of::<T>()),
631+
_marker: PhantomData,
632+
}
620633
}
621634

622635
/// Creates an empty deque with space for at least `capacity` elements.
@@ -630,7 +643,12 @@ impl<T, A: Allocator> VecDeque<T, A> {
630643
/// ```
631644
#[unstable(feature = "allocator_api", issue = "32838")]
632645
pub fn with_capacity_in(capacity: usize, alloc: A) -> VecDeque<T, A> {
633-
VecDeque { head: 0, len: 0, buf: RawVec::with_capacity_in(capacity, alloc) }
646+
VecDeque {
647+
head: 0,
648+
len: 0,
649+
buf: RawVec::with_capacity_in(capacity, alloc),
650+
_marker: PhantomData,
651+
}
634652
}
635653

636654
/// Creates a `VecDeque` from a raw allocation, when the initialized
@@ -661,6 +679,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
661679
head: initialized.start,
662680
len: initialized.end.unchecked_sub(initialized.start),
663681
buf: RawVec::from_raw_parts_in(ptr, capacity, alloc),
682+
_marker: PhantomData,
664683
}
665684
}
666685
}
@@ -767,7 +786,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
767786
#[inline]
768787
#[stable(feature = "rust1", since = "1.0.0")]
769788
pub fn capacity(&self) -> usize {
770-
if T::IS_ZST { usize::MAX } else { self.buf.capacity() }
789+
self.buf.capacity()
771790
}
772791

773792
/// Reserves the minimum capacity for at least `additional` more elements to be inserted in the
@@ -2980,7 +2999,12 @@ impl<T, A: Allocator> From<Vec<T, A>> for VecDeque<T, A> {
29802999
#[inline]
29813000
fn from(other: Vec<T, A>) -> Self {
29823001
let (ptr, len, cap, alloc) = other.into_raw_parts_with_alloc();
2983-
Self { head: 0, len, buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) } }
3002+
Self {
3003+
head: 0,
3004+
len,
3005+
buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) },
3006+
_marker: PhantomData,
3007+
}
29843008
}
29853009
}
29863010

@@ -3020,7 +3044,7 @@ impl<T, A: Allocator> From<VecDeque<T, A>> for Vec<T, A> {
30203044

30213045
unsafe {
30223046
let other = ManuallyDrop::new(other);
3023-
let buf = other.buf.ptr();
3047+
let buf: *mut T = other.buf.ptr();
30243048
let len = other.len();
30253049
let cap = other.capacity();
30263050
let alloc = ptr::read(other.allocator());

0 commit comments

Comments
 (0)