11use crate :: clone:: TrivialClone ;
22use crate :: cmp:: Ordering ;
33use crate :: marker:: { Destruct , PointeeSized , Unsize } ;
4- use crate :: mem:: { MaybeUninit , SizedTypeProperties } ;
4+ use crate :: mem:: { MaybeUninit , SizedTypeProperties , transmute } ;
55use crate :: num:: NonZero ;
66use crate :: ops:: { CoerceUnsized , DispatchFromDyn } ;
77use crate :: pin:: PinCoerceUnsized ;
@@ -100,9 +100,8 @@ impl<T: Sized> NonNull<T> {
100100 #[ must_use]
101101 #[ inline]
102102 pub const fn without_provenance ( addr : NonZero < usize > ) -> Self {
103- let pointer = crate :: ptr:: without_provenance ( addr. get ( ) ) ;
104- // SAFETY: we know `addr` is non-zero.
105- unsafe { NonNull { pointer } }
103+ // SAFETY: we know `addr` is non-zero and all nonzero integers are valid raw pointers.
104+ unsafe { transmute ( addr) }
106105 }
107106
108107 /// Creates a new `NonNull` that is dangling, but well-aligned.
@@ -239,7 +238,7 @@ impl<T: PointeeSized> NonNull<T> {
239238 "NonNull::new_unchecked requires that the pointer is non-null" ,
240239 ( ptr: * mut ( ) = ptr as * mut ( ) ) => !ptr. is_null( )
241240 ) ;
242- NonNull { pointer : ptr as _ }
241+ transmute ( ptr)
243242 }
244243 }
245244
@@ -282,7 +281,7 @@ impl<T: PointeeSized> NonNull<T> {
282281 #[ inline]
283282 pub const fn from_ref ( r : & T ) -> Self {
284283 // SAFETY: A reference cannot be null.
285- unsafe { NonNull { pointer : r as * const T } }
284+ unsafe { transmute ( r as * const T ) }
286285 }
287286
288287 /// Converts a mutable reference to a `NonNull` pointer.
@@ -291,7 +290,7 @@ impl<T: PointeeSized> NonNull<T> {
291290 #[ inline]
292291 pub const fn from_mut ( r : & mut T ) -> Self {
293292 // SAFETY: A mutable reference cannot be null.
294- unsafe { NonNull { pointer : r as * mut T } }
293+ unsafe { transmute ( r as * mut T ) }
295294 }
296295
297296 /// Performs the same functionality as [`std::ptr::from_raw_parts`], except that a
@@ -502,7 +501,7 @@ impl<T: PointeeSized> NonNull<T> {
502501 #[ inline]
503502 pub const fn cast < U > ( self ) -> NonNull < U > {
504503 // SAFETY: `self` is a `NonNull` pointer which is necessarily non-null
505- unsafe { NonNull { pointer : self . as_ptr ( ) as * mut U } }
504+ unsafe { transmute ( self . as_ptr ( ) as * mut U ) }
506505 }
507506
508507 /// Try to cast to a pointer of another type by checking alignment.
@@ -581,7 +580,7 @@ impl<T: PointeeSized> NonNull<T> {
581580 // Additionally safety contract of `offset` guarantees that the resulting pointer is
582581 // pointing to an allocation, there can't be an allocation at null, thus it's safe to
583582 // construct `NonNull`.
584- unsafe { NonNull { pointer : intrinsics:: offset ( self . as_ptr ( ) , count) } }
583+ unsafe { transmute ( intrinsics:: offset ( self . as_ptr ( ) , count) ) }
585584 }
586585
587586 /// Calculates the offset from a pointer in bytes.
@@ -605,7 +604,7 @@ impl<T: PointeeSized> NonNull<T> {
605604 // Additionally safety contract of `offset` guarantees that the resulting pointer is
606605 // pointing to an allocation, there can't be an allocation at null, thus it's safe to
607606 // construct `NonNull`.
608- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_offset ( count) } }
607+ unsafe { transmute ( self . as_ptr ( ) . byte_offset ( count) ) }
609608 }
610609
611610 /// Adds an offset to a pointer (convenience for `.offset(count as isize)`).
@@ -657,7 +656,7 @@ impl<T: PointeeSized> NonNull<T> {
657656 // Additionally safety contract of `offset` guarantees that the resulting pointer is
658657 // pointing to an allocation, there can't be an allocation at null, thus it's safe to
659658 // construct `NonNull`.
660- unsafe { NonNull { pointer : intrinsics:: offset ( self . as_ptr ( ) , count) } }
659+ unsafe { transmute ( intrinsics:: offset ( self . as_ptr ( ) , count) ) }
661660 }
662661
663662 /// Calculates the offset from a pointer in bytes (convenience for `.byte_offset(count as isize)`).
@@ -681,7 +680,7 @@ impl<T: PointeeSized> NonNull<T> {
681680 // Additionally safety contract of `add` guarantees that the resulting pointer is pointing
682681 // to an allocation, there can't be an allocation at null, thus it's safe to construct
683682 // `NonNull`.
684- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_add ( count) } }
683+ unsafe { transmute ( self . as_ptr ( ) . byte_add ( count) ) }
685684 }
686685
687686 /// Subtracts an offset from a pointer (convenience for
@@ -763,7 +762,7 @@ impl<T: PointeeSized> NonNull<T> {
763762 // Additionally safety contract of `sub` guarantees that the resulting pointer is pointing
764763 // to an allocation, there can't be an allocation at null, thus it's safe to construct
765764 // `NonNull`.
766- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_sub ( count) } }
765+ unsafe { transmute ( self . as_ptr ( ) . byte_sub ( count) ) }
767766 }
768767
769768 /// Calculates the distance between two pointers within the same allocation. The returned value is in
0 commit comments