Skip to content

Commit 88d6e9f

Browse files
committed
Reduce use of NonNull::new_unchecked in library/
1 parent b0ea682 commit 88d6e9f

File tree

6 files changed

+24
-24
lines changed

6 files changed

+24
-24
lines changed

library/alloc/src/raw_vec.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,7 @@ impl<T, A: Allocator> RawVec<T, A> {
207207
// Allocators currently return a `NonNull<[u8]>` whose length
208208
// matches the size requested. If that ever changes, the capacity
209209
// here should change to `ptr.len() / mem::size_of::<T>()`.
210-
Self {
211-
ptr: unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) },
212-
cap: unsafe { Cap(capacity) },
213-
alloc,
214-
}
210+
Self { ptr: Unique::from(ptr.cast()), cap: unsafe { Cap(capacity) }, alloc }
215211
}
216212
}
217213

@@ -239,6 +235,11 @@ impl<T, A: Allocator> RawVec<T, A> {
239235
self.ptr.as_ptr()
240236
}
241237

238+
#[inline]
239+
pub fn non_null(&self) -> NonNull<T> {
240+
NonNull::from(self.ptr)
241+
}
242+
242243
/// Gets the capacity of the allocation.
243244
///
244245
/// This will always be `usize::MAX` if `T` is zero-sized.
@@ -398,7 +399,7 @@ impl<T, A: Allocator> RawVec<T, A> {
398399
// Allocators currently return a `NonNull<[u8]>` whose length matches
399400
// the size requested. If that ever changes, the capacity here should
400401
// change to `ptr.len() / mem::size_of::<T>()`.
401-
self.ptr = unsafe { Unique::new_unchecked(ptr.cast().as_ptr()) };
402+
self.ptr = Unique::from(ptr.cast());
402403
self.cap = unsafe { Cap(cap) };
403404
}
404405

library/alloc/src/vec/into_iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl<T, A: Allocator> IntoIter<T, A> {
136136
// struct and then overwriting &mut self.
137137
// this creates less assembly
138138
self.cap = 0;
139-
self.buf = unsafe { NonNull::new_unchecked(RawVec::NEW.ptr()) };
139+
self.buf = RawVec::NEW.non_null();
140140
self.ptr = self.buf;
141141
self.end = self.buf.as_ptr();
142142

library/alloc/src/vec/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2861,16 +2861,16 @@ impl<T, A: Allocator> IntoIterator for Vec<T, A> {
28612861
#[inline]
28622862
fn into_iter(self) -> Self::IntoIter {
28632863
unsafe {
2864-
let mut me = ManuallyDrop::new(self);
2864+
let me = ManuallyDrop::new(self);
28652865
let alloc = ManuallyDrop::new(ptr::read(me.allocator()));
2866-
let begin = me.as_mut_ptr();
2866+
let buf = me.buf.non_null();
2867+
let begin = buf.as_ptr();
28672868
let end = if T::IS_ZST {
28682869
begin.wrapping_byte_add(me.len())
28692870
} else {
28702871
begin.add(me.len()) as *const T
28712872
};
28722873
let cap = me.buf.capacity();
2873-
let buf = NonNull::new_unchecked(begin);
28742874
IntoIter { buf, phantom: PhantomData, cap, alloc, ptr: buf, end }
28752875
}
28762876
}

library/core/src/ptr/non_null.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ impl<T: ?Sized> NonNull<T> {
473473
#[inline]
474474
pub const fn cast<U>(self) -> NonNull<U> {
475475
// SAFETY: `self` is a `NonNull` pointer which is necessarily non-null
476-
unsafe { NonNull::new_unchecked(self.as_ptr() as *mut U) }
476+
unsafe { NonNull { pointer: self.as_ptr() as *mut U } }
477477
}
478478

479479
/// Calculates the offset from a pointer.
@@ -1828,9 +1828,8 @@ impl<T: ?Sized> hash::Hash for NonNull<T> {
18281828
impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
18291829
#[inline]
18301830
fn from(unique: Unique<T>) -> Self {
1831-
// SAFETY: A Unique pointer cannot be null, so the conditions for
1832-
// new_unchecked() are respected.
1833-
unsafe { NonNull::new_unchecked(unique.as_ptr()) }
1831+
// SAFETY: A Unique pointer cannot be null.
1832+
unsafe { NonNull { pointer: unique.as_ptr() } }
18341833
}
18351834
}
18361835

@@ -1853,8 +1852,7 @@ impl<T: ?Sized> From<&T> for NonNull<T> {
18531852
/// This conversion is safe and infallible since references cannot be null.
18541853
#[inline]
18551854
fn from(reference: &T) -> Self {
1856-
// SAFETY: A reference cannot be null, so the conditions for
1857-
// new_unchecked() are respected.
1855+
// SAFETY: A reference cannot be null.
18581856
unsafe { NonNull { pointer: reference as *const T } }
18591857
}
18601858
}

library/core/src/ptr/unique.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl<T: ?Sized> Unique<T> {
138138
pub const fn cast<U>(self) -> Unique<U> {
139139
// FIXME(const-hack): replace with `From`
140140
// SAFETY: is `NonNull`
141-
unsafe { Unique::new_unchecked(self.pointer.cast().as_ptr()) }
141+
Unique { pointer: self.pointer.cast(), _marker: PhantomData }
142142
}
143143
}
144144

library/core/src/slice/iter.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,13 @@ unsafe impl<T: Sync> Send for Iter<'_, T> {}
8787
impl<'a, T> Iter<'a, T> {
8888
#[inline]
8989
pub(super) fn new(slice: &'a [T]) -> Self {
90-
let ptr = slice.as_ptr();
90+
let len = slice.len();
91+
let ptr: NonNull<T> = NonNull::from(slice).cast();
9192
// SAFETY: Similar to `IterMut::new`.
9293
unsafe {
93-
let end_or_len = if T::IS_ZST { invalid(slice.len()) } else { ptr.add(slice.len()) };
94+
let end_or_len = if T::IS_ZST { invalid(len) } else { ptr.as_ptr().add(len) };
9495

95-
Self { ptr: NonNull::new_unchecked(ptr as *mut T), end_or_len, _marker: PhantomData }
96+
Self { ptr, end_or_len, _marker: PhantomData }
9697
}
9798
}
9899

@@ -208,7 +209,8 @@ unsafe impl<T: Send> Send for IterMut<'_, T> {}
208209
impl<'a, T> IterMut<'a, T> {
209210
#[inline]
210211
pub(super) fn new(slice: &'a mut [T]) -> Self {
211-
let ptr = slice.as_mut_ptr();
212+
let len = slice.len();
213+
let ptr: NonNull<T> = NonNull::from(slice).cast();
212214
// SAFETY: There are several things here:
213215
//
214216
// `ptr` has been obtained by `slice.as_ptr()` where `slice` is a valid
@@ -226,10 +228,9 @@ impl<'a, T> IterMut<'a, T> {
226228
// See the `next_unchecked!` and `is_empty!` macros as well as the
227229
// `post_inc_start` method for more information.
228230
unsafe {
229-
let end_or_len =
230-
if T::IS_ZST { invalid_mut(slice.len()) } else { ptr.add(slice.len()) };
231+
let end_or_len = if T::IS_ZST { invalid_mut(len) } else { ptr.as_ptr().add(len) };
231232

232-
Self { ptr: NonNull::new_unchecked(ptr), end_or_len, _marker: PhantomData }
233+
Self { ptr, end_or_len, _marker: PhantomData }
233234
}
234235
}
235236

0 commit comments

Comments
 (0)