Reduce use of NonNull::new_unchecked in library/
This commit is contained in:
parent
b0ea682a2c
commit
88d6e9f868
@ -207,11 +207,7 @@ fn allocate_in(capacity: usize, init: AllocInit, alloc: A) -> Self {
|
|||||||
// Allocators currently return a `NonNull<[u8]>` whose length
|
// Allocators currently return a `NonNull<[u8]>` whose length
|
||||||
// matches the size requested. If that ever changes, the capacity
|
// matches the size requested. If that ever changes, the capacity
|
||||||
// here should change to `ptr.len() / mem::size_of::<T>()`.
|
// here should change to `ptr.len() / mem::size_of::<T>()`.
|
||||||
Self {
|
Self { ptr: Unique::from(ptr.cast()), cap: unsafe { Cap(capacity) }, alloc }
|
||||||
ptr: unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) },
|
|
||||||
cap: unsafe { Cap(capacity) },
|
|
||||||
alloc,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -239,6 +235,11 @@ pub fn ptr(&self) -> *mut T {
|
|||||||
self.ptr.as_ptr()
|
self.ptr.as_ptr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn non_null(&self) -> NonNull<T> {
|
||||||
|
NonNull::from(self.ptr)
|
||||||
|
}
|
||||||
|
|
||||||
/// Gets the capacity of the allocation.
|
/// Gets the capacity of the allocation.
|
||||||
///
|
///
|
||||||
/// This will always be `usize::MAX` if `T` is zero-sized.
|
/// This will always be `usize::MAX` if `T` is zero-sized.
|
||||||
@ -398,7 +399,7 @@ unsafe fn set_ptr_and_cap(&mut self, ptr: NonNull<[u8]>, cap: usize) {
|
|||||||
// Allocators currently return a `NonNull<[u8]>` whose length matches
|
// Allocators currently return a `NonNull<[u8]>` whose length matches
|
||||||
// the size requested. If that ever changes, the capacity here should
|
// the size requested. If that ever changes, the capacity here should
|
||||||
// change to `ptr.len() / mem::size_of::<T>()`.
|
// change to `ptr.len() / mem::size_of::<T>()`.
|
||||||
self.ptr = unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) };
|
self.ptr = Unique::from(ptr.cast());
|
||||||
self.cap = unsafe { Cap(cap) };
|
self.cap = unsafe { Cap(cap) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,7 +136,7 @@ pub(super) fn forget_allocation_drop_remaining(&mut self) {
|
|||||||
// struct and then overwriting &mut self.
|
// struct and then overwriting &mut self.
|
||||||
// this creates less assembly
|
// this creates less assembly
|
||||||
self.cap = 0;
|
self.cap = 0;
|
||||||
self.buf = unsafe { NonNull::new_unchecked(RawVec::NEW.ptr()) };
|
self.buf = RawVec::NEW.non_null();
|
||||||
self.ptr = self.buf;
|
self.ptr = self.buf;
|
||||||
self.end = self.buf.as_ptr();
|
self.end = self.buf.as_ptr();
|
||||||
|
|
||||||
|
@ -2861,16 +2861,16 @@ impl<T, A: Allocator> IntoIterator for Vec<T, A> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
unsafe {
|
unsafe {
|
||||||
let mut me = ManuallyDrop::new(self);
|
let me = ManuallyDrop::new(self);
|
||||||
let alloc = ManuallyDrop::new(ptr::read(me.allocator()));
|
let alloc = ManuallyDrop::new(ptr::read(me.allocator()));
|
||||||
let begin = me.as_mut_ptr();
|
let buf = me.buf.non_null();
|
||||||
|
let begin = buf.as_ptr();
|
||||||
let end = if T::IS_ZST {
|
let end = if T::IS_ZST {
|
||||||
begin.wrapping_byte_add(me.len())
|
begin.wrapping_byte_add(me.len())
|
||||||
} else {
|
} else {
|
||||||
begin.add(me.len()) as *const T
|
begin.add(me.len()) as *const T
|
||||||
};
|
};
|
||||||
let cap = me.buf.capacity();
|
let cap = me.buf.capacity();
|
||||||
let buf = NonNull::new_unchecked(begin);
|
|
||||||
IntoIter { buf, phantom: PhantomData, cap, alloc, ptr: buf, end }
|
IntoIter { buf, phantom: PhantomData, cap, alloc, ptr: buf, end }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -473,7 +473,7 @@ pub const fn as_ptr(self) -> *mut T {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub const fn cast<U>(self) -> NonNull<U> {
|
pub const fn cast<U>(self) -> NonNull<U> {
|
||||||
// SAFETY: `self` is a `NonNull` pointer which is necessarily non-null
|
// SAFETY: `self` is a `NonNull` pointer which is necessarily non-null
|
||||||
unsafe { NonNull::new_unchecked(self.as_ptr() as *mut U) }
|
unsafe { NonNull { pointer: self.as_ptr() as *mut U } }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calculates the offset from a pointer.
|
/// Calculates the offset from a pointer.
|
||||||
@ -1828,9 +1828,8 @@ fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
|||||||
impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
|
impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from(unique: Unique<T>) -> Self {
|
fn from(unique: Unique<T>) -> Self {
|
||||||
// SAFETY: A Unique pointer cannot be null, so the conditions for
|
// SAFETY: A Unique pointer cannot be null.
|
||||||
// new_unchecked() are respected.
|
unsafe { NonNull { pointer: unique.as_ptr() } }
|
||||||
unsafe { NonNull::new_unchecked(unique.as_ptr()) }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1853,8 +1852,7 @@ impl<T: ?Sized> From<&T> for NonNull<T> {
|
|||||||
/// This conversion is safe and infallible since references cannot be null.
|
/// This conversion is safe and infallible since references cannot be null.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from(reference: &T) -> Self {
|
fn from(reference: &T) -> Self {
|
||||||
// SAFETY: A reference cannot be null, so the conditions for
|
// SAFETY: A reference cannot be null.
|
||||||
// new_unchecked() are respected.
|
|
||||||
unsafe { NonNull { pointer: reference as *const T } }
|
unsafe { NonNull { pointer: reference as *const T } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -138,7 +138,7 @@ pub const fn as_ptr(self) -> *mut T {
|
|||||||
pub const fn cast<U>(self) -> Unique<U> {
|
pub const fn cast<U>(self) -> Unique<U> {
|
||||||
// FIXME(const-hack): replace with `From`
|
// FIXME(const-hack): replace with `From`
|
||||||
// SAFETY: is `NonNull`
|
// SAFETY: is `NonNull`
|
||||||
unsafe { Unique::new_unchecked(self.pointer.cast().as_ptr()) }
|
Unique { pointer: self.pointer.cast(), _marker: PhantomData }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,12 +87,13 @@ unsafe impl<T: Sync> Send for Iter<'_, T> {}
|
|||||||
impl<'a, T> Iter<'a, T> {
|
impl<'a, T> Iter<'a, T> {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(super) fn new(slice: &'a [T]) -> Self {
|
pub(super) fn new(slice: &'a [T]) -> Self {
|
||||||
let ptr = slice.as_ptr();
|
let len = slice.len();
|
||||||
|
let ptr: NonNull<T> = NonNull::from(slice).cast();
|
||||||
// SAFETY: Similar to `IterMut::new`.
|
// SAFETY: Similar to `IterMut::new`.
|
||||||
unsafe {
|
unsafe {
|
||||||
let end_or_len = if T::IS_ZST { invalid(slice.len()) } else { ptr.add(slice.len()) };
|
let end_or_len = if T::IS_ZST { invalid(len) } else { ptr.as_ptr().add(len) };
|
||||||
|
|
||||||
Self { ptr: NonNull::new_unchecked(ptr as *mut T), end_or_len, _marker: PhantomData }
|
Self { ptr, end_or_len, _marker: PhantomData }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,7 +209,8 @@ unsafe impl<T: Send> Send for IterMut<'_, T> {}
|
|||||||
impl<'a, T> IterMut<'a, T> {
|
impl<'a, T> IterMut<'a, T> {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(super) fn new(slice: &'a mut [T]) -> Self {
|
pub(super) fn new(slice: &'a mut [T]) -> Self {
|
||||||
let ptr = slice.as_mut_ptr();
|
let len = slice.len();
|
||||||
|
let ptr: NonNull<T> = NonNull::from(slice).cast();
|
||||||
// SAFETY: There are several things here:
|
// SAFETY: There are several things here:
|
||||||
//
|
//
|
||||||
// `ptr` has been obtained by `slice.as_ptr()` where `slice` is a valid
|
// `ptr` has been obtained by `slice.as_ptr()` where `slice` is a valid
|
||||||
@ -226,10 +228,9 @@ pub(super) fn new(slice: &'a mut [T]) -> Self {
|
|||||||
// See the `next_unchecked!` and `is_empty!` macros as well as the
|
// See the `next_unchecked!` and `is_empty!` macros as well as the
|
||||||
// `post_inc_start` method for more information.
|
// `post_inc_start` method for more information.
|
||||||
unsafe {
|
unsafe {
|
||||||
let end_or_len =
|
let end_or_len = if T::IS_ZST { invalid_mut(len) } else { ptr.as_ptr().add(len) };
|
||||||
if T::IS_ZST { invalid_mut(slice.len()) } else { ptr.add(slice.len()) };
|
|
||||||
|
|
||||||
Self { ptr: NonNull::new_unchecked(ptr), end_or_len, _marker: PhantomData }
|
Self { ptr, end_or_len, _marker: PhantomData }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user