mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-27 18:57:42 +03:00
constify Vec::{into, from}_raw_parts{_in|_alloc}
This commit is contained in:
@@ -43,7 +43,7 @@ enum AllocInit {
|
||||
/// `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) } }
|
||||
}
|
||||
|
||||
@@ -260,7 +260,7 @@ pub(crate) unsafe fn into_box(self, len: usize) -> Box<[MaybeUninit<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();
|
||||
@@ -278,7 +278,8 @@ pub(crate) unsafe fn from_raw_parts_in(ptr: *mut T, capacity: usize, alloc: 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();
|
||||
@@ -310,7 +311,7 @@ pub(crate) const fn capacity(&self) -> usize {
|
||||
|
||||
/// 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()
|
||||
}
|
||||
|
||||
@@ -593,12 +594,13 @@ fn with_capacity_zeroed_in(capacity: usize, alloc: A, elem_layout: Layout) -> Se
|
||||
}
|
||||
|
||||
#[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 }
|
||||
}
|
||||
|
||||
@@ -618,7 +620,7 @@ const fn capacity(&self, elem_size: usize) -> usize {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn allocator(&self) -> &A {
|
||||
const fn allocator(&self) -> &A {
|
||||
&self.alloc
|
||||
}
|
||||
|
||||
|
||||
@@ -640,7 +640,8 @@ pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
|
||||
/// ```
|
||||
#[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) }
|
||||
}
|
||||
|
||||
@@ -742,7 +743,8 @@ pub unsafe fn from_raw_parts(ptr: *mut T, length: usize, capacity: usize) -> Sel
|
||||
/// ```
|
||||
#[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) }
|
||||
}
|
||||
|
||||
@@ -836,7 +838,8 @@ pub fn from_fn<F>(length: usize, f: F) -> Self
|
||||
/// ```
|
||||
#[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())
|
||||
}
|
||||
@@ -877,7 +880,8 @@ pub fn into_raw_parts(self) -> (*mut T, usize, usize) {
|
||||
/// ```
|
||||
#[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)
|
||||
@@ -1178,7 +1182,13 @@ pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserv
|
||||
/// ```
|
||||
#[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",
|
||||
@@ -1287,8 +1297,14 @@ pub unsafe fn from_raw_parts_in(ptr: *mut T, length: usize, capacity: usize, all
|
||||
/// ```
|
||||
#[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",
|
||||
@@ -1336,7 +1352,8 @@ pub unsafe fn from_parts_in(ptr: NonNull<T>, length: usize, capacity: usize, all
|
||||
/// ```
|
||||
#[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();
|
||||
@@ -1385,8 +1402,9 @@ pub fn into_raw_parts_with_alloc(self) -> (*mut T, usize, usize, 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)
|
||||
@@ -2065,8 +2083,9 @@ pub const fn as_non_null(&mut self) -> NonNull<T> {
|
||||
|
||||
/// 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()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user