Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions library/alloc/src/raw_vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const ZERO_CAP: Cap = unsafe { Cap::new_unchecked(0) };
/// `Cap(cap)`, except if `T` is a ZST then `Cap::ZERO`.
///
/// # Safety: cap must be <= `isize::MAX`.
unsafe fn new_cap<T>(cap: usize) -> Cap {
const unsafe fn new_cap<T>(cap: usize) -> Cap {
if T::IS_ZST { ZERO_CAP } else { unsafe { Cap::new_unchecked(cap) } }
}

Expand Down Expand Up @@ -260,7 +260,7 @@ impl<T, A: Allocator> RawVec<T, A> {
/// If the `ptr` and `capacity` come from a `RawVec` created via `alloc`, then this is
/// guaranteed.
#[inline]
pub(crate) unsafe fn from_raw_parts_in(ptr: *mut T, capacity: usize, alloc: A) -> Self {
pub(crate) const unsafe fn from_raw_parts_in(ptr: *mut T, capacity: usize, alloc: A) -> Self {
// SAFETY: Precondition passed to the caller
unsafe {
let ptr = ptr.cast();
Expand All @@ -278,7 +278,8 @@ impl<T, A: Allocator> RawVec<T, A> {
///
/// See [`RawVec::from_raw_parts_in`].
#[inline]
pub(crate) unsafe fn from_nonnull_in(ptr: NonNull<T>, capacity: usize, alloc: A) -> Self {
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
pub(crate) const unsafe fn from_nonnull_in(ptr: NonNull<T>, capacity: usize, alloc: A) -> Self {
// SAFETY: Precondition passed to the caller
unsafe {
let ptr = ptr.cast();
Expand Down Expand Up @@ -310,7 +311,7 @@ impl<T, A: Allocator> RawVec<T, A> {

/// Returns a shared reference to the allocator backing this `RawVec`.
#[inline]
pub(crate) fn allocator(&self) -> &A {
pub(crate) const fn allocator(&self) -> &A {
self.inner.allocator()
}

Expand Down Expand Up @@ -593,12 +594,13 @@ impl<A: Allocator> RawVecInner<A> {
}

#[inline]
unsafe fn from_raw_parts_in(ptr: *mut u8, cap: Cap, alloc: A) -> Self {
const unsafe fn from_raw_parts_in(ptr: *mut u8, cap: Cap, alloc: A) -> Self {
Self { ptr: unsafe { Unique::new_unchecked(ptr) }, cap, alloc }
}

#[inline]
unsafe fn from_nonnull_in(ptr: NonNull<u8>, cap: Cap, alloc: A) -> Self {
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
const unsafe fn from_nonnull_in(ptr: NonNull<u8>, cap: Cap, alloc: A) -> Self {
Self { ptr: Unique::from(ptr), cap, alloc }
}

Expand All @@ -618,7 +620,7 @@ impl<A: Allocator> RawVecInner<A> {
}

#[inline]
fn allocator(&self) -> &A {
const fn allocator(&self) -> &A {
&self.alloc
}

Expand Down
37 changes: 28 additions & 9 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,8 @@ impl<T> Vec<T> {
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn from_raw_parts(ptr: *mut T, length: usize, capacity: usize) -> Self {
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
pub const unsafe fn from_raw_parts(ptr: *mut T, length: usize, capacity: usize) -> Self {
unsafe { Self::from_raw_parts_in(ptr, length, capacity, Global) }
}

Expand Down Expand Up @@ -742,7 +743,8 @@ impl<T> Vec<T> {
/// ```
#[inline]
#[unstable(feature = "box_vec_non_null", issue = "130364")]
pub unsafe fn from_parts(ptr: NonNull<T>, length: usize, capacity: usize) -> Self {
#[rustc_const_unstable(feature = "box_vec_non_null", issue = "130364")]
pub const unsafe fn from_parts(ptr: NonNull<T>, length: usize, capacity: usize) -> Self {
unsafe { Self::from_parts_in(ptr, length, capacity, Global) }
}

Expand Down Expand Up @@ -836,7 +838,8 @@ impl<T> Vec<T> {
/// ```
#[must_use = "losing the pointer will leak memory"]
#[stable(feature = "vec_into_raw_parts", since = "1.93.0")]
pub fn into_raw_parts(self) -> (*mut T, usize, usize) {
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
pub const fn into_raw_parts(self) -> (*mut T, usize, usize) {
let mut me = ManuallyDrop::new(self);
(me.as_mut_ptr(), me.len(), me.capacity())
}
Expand Down Expand Up @@ -877,7 +880,8 @@ impl<T> Vec<T> {
/// ```
#[must_use = "losing the pointer will leak memory"]
#[unstable(feature = "box_vec_non_null", issue = "130364")]
pub fn into_parts(self) -> (NonNull<T>, usize, usize) {
#[rustc_const_unstable(feature = "box_vec_non_null", issue = "130364")]
pub const fn into_parts(self) -> (NonNull<T>, usize, usize) {
let (ptr, len, capacity) = self.into_raw_parts();
// SAFETY: A `Vec` always has a non-null pointer.
(unsafe { NonNull::new_unchecked(ptr) }, len, capacity)
Expand Down Expand Up @@ -1178,7 +1182,13 @@ impl<T, A: Allocator> Vec<T, A> {
/// ```
#[inline]
#[unstable(feature = "allocator_api", issue = "32838")]
pub unsafe fn from_raw_parts_in(ptr: *mut T, length: usize, capacity: usize, alloc: A) -> Self {
#[rustc_const_unstable(feature = "allocator_api", issue = "32838")]
pub const unsafe fn from_raw_parts_in(
ptr: *mut T,
length: usize,
capacity: usize,
alloc: A,
) -> Self {
ub_checks::assert_unsafe_precondition!(
check_library_ub,
"Vec::from_raw_parts_in requires that length <= capacity",
Expand Down Expand Up @@ -1287,8 +1297,14 @@ impl<T, A: Allocator> Vec<T, A> {
/// ```
#[inline]
#[unstable(feature = "allocator_api", issue = "32838")]
#[rustc_const_unstable(feature = "allocator_api", issue = "32838")]
// #[unstable(feature = "box_vec_non_null", issue = "130364")]
pub unsafe fn from_parts_in(ptr: NonNull<T>, length: usize, capacity: usize, alloc: A) -> Self {
pub const unsafe fn from_parts_in(
ptr: NonNull<T>,
length: usize,
capacity: usize,
alloc: A,
) -> Self {
ub_checks::assert_unsafe_precondition!(
check_library_ub,
"Vec::from_parts_in requires that length <= capacity",
Expand Down Expand Up @@ -1336,7 +1352,8 @@ impl<T, A: Allocator> Vec<T, A> {
/// ```
#[must_use = "losing the pointer will leak memory"]
#[unstable(feature = "allocator_api", issue = "32838")]
pub fn into_raw_parts_with_alloc(self) -> (*mut T, usize, usize, A) {
#[rustc_const_unstable(feature = "allocator_api", issue = "32838")]
pub const fn into_raw_parts_with_alloc(self) -> (*mut T, usize, usize, A) {
let mut me = ManuallyDrop::new(self);
let len = me.len();
let capacity = me.capacity();
Expand Down Expand Up @@ -1385,8 +1402,9 @@ impl<T, A: Allocator> Vec<T, A> {
/// ```
#[must_use = "losing the pointer will leak memory"]
#[unstable(feature = "allocator_api", issue = "32838")]
#[rustc_const_unstable(feature = "allocator_api", issue = "32838")]
// #[unstable(feature = "box_vec_non_null", issue = "130364")]
pub fn into_parts_with_alloc(self) -> (NonNull<T>, usize, usize, A) {
pub const fn into_parts_with_alloc(self) -> (NonNull<T>, usize, usize, A) {
let (ptr, len, capacity, alloc) = self.into_raw_parts_with_alloc();
// SAFETY: A `Vec` always has a non-null pointer.
(unsafe { NonNull::new_unchecked(ptr) }, len, capacity, alloc)
Expand Down Expand Up @@ -2065,8 +2083,9 @@ impl<T, A: Allocator> Vec<T, A> {

/// Returns a reference to the underlying allocator.
#[unstable(feature = "allocator_api", issue = "32838")]
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
#[inline]
pub fn allocator(&self) -> &A {
pub const fn allocator(&self) -> &A {
self.buf.allocator()
}

Expand Down
Loading