11use crate :: cmp:: Ordering ;
22use crate :: marker:: { PointeeSized , Unsize } ;
3- use crate :: mem:: { MaybeUninit , SizedTypeProperties } ;
3+ use crate :: mem:: { MaybeUninit , SizedTypeProperties , transmute } ;
44use crate :: num:: NonZero ;
55use crate :: ops:: { CoerceUnsized , DispatchFromDyn } ;
66use crate :: pin:: PinCoerceUnsized ;
@@ -69,13 +69,10 @@ use crate::{fmt, hash, intrinsics, mem, ptr};
6969/// [null pointer optimization]: crate::option#representation
7070#[ stable( feature = "nonnull" , since = "1.25.0" ) ]
7171#[ repr( transparent) ]
72- #[ rustc_layout_scalar_valid_range_start( 1 ) ]
7372#[ rustc_nonnull_optimization_guaranteed]
7473#[ rustc_diagnostic_item = "NonNull" ]
7574pub struct NonNull < T : PointeeSized > {
76- // Remember to use `.as_ptr()` instead of `.pointer`, as field projecting to
77- // this is banned by <https://github.com/rust-lang/compiler-team/issues/807>.
78- pointer : * const T ,
75+ pointer : crate :: pattern_type!( * const T is !null) ,
7976}
8077
8178/// `NonNull` pointers are not `Send` because the data they reference may be aliased.
@@ -99,9 +96,9 @@ impl<T: Sized> NonNull<T> {
9996 #[ must_use]
10097 #[ inline]
10198 pub const fn without_provenance ( addr : NonZero < usize > ) -> Self {
102- let pointer = crate :: ptr:: without_provenance ( addr. get ( ) ) ;
99+ let pointer: * const T = crate :: ptr:: without_provenance ( addr. get ( ) ) ;
103100 // SAFETY: we know `addr` is non-zero.
104- unsafe { NonNull { pointer } }
101+ unsafe { NonNull { pointer : transmute ( pointer ) } }
105102 }
106103
107104 /// Creates a new `NonNull` that is dangling, but well-aligned.
@@ -231,7 +228,7 @@ impl<T: PointeeSized> NonNull<T> {
231228 "NonNull::new_unchecked requires that the pointer is non-null" ,
232229 ( ptr: * mut ( ) = ptr as * mut ( ) ) => !ptr. is_null( )
233230 ) ;
234- NonNull { pointer : ptr as _ }
231+ NonNull { pointer : transmute ( ptr) }
235232 }
236233 }
237234
@@ -274,7 +271,7 @@ impl<T: PointeeSized> NonNull<T> {
274271 #[ inline]
275272 pub const fn from_ref ( r : & T ) -> Self {
276273 // SAFETY: A reference cannot be null.
277- unsafe { NonNull { pointer : r as * const T } }
274+ unsafe { NonNull { pointer : transmute ( r as * const T ) } }
278275 }
279276
280277 /// Converts a mutable reference to a `NonNull` pointer.
@@ -283,7 +280,7 @@ impl<T: PointeeSized> NonNull<T> {
283280 #[ inline]
284281 pub const fn from_mut ( r : & mut T ) -> Self {
285282 // SAFETY: A mutable reference cannot be null.
286- unsafe { NonNull { pointer : r as * mut T } }
283+ unsafe { NonNull { pointer : transmute ( r as * mut T ) } }
287284 }
288285
289286 /// Performs the same functionality as [`std::ptr::from_raw_parts`], except that a
@@ -494,7 +491,7 @@ impl<T: PointeeSized> NonNull<T> {
494491 #[ inline]
495492 pub const fn cast < U > ( self ) -> NonNull < U > {
496493 // SAFETY: `self` is a `NonNull` pointer which is necessarily non-null
497- unsafe { NonNull { pointer : self . as_ptr ( ) as * mut U } }
494+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) as * mut U ) } }
498495 }
499496
500497 /// Try to cast to a pointer of another type by checking alignment.
@@ -573,7 +570,7 @@ impl<T: PointeeSized> NonNull<T> {
573570 // Additionally safety contract of `offset` guarantees that the resulting pointer is
574571 // pointing to an allocation, there can't be an allocation at null, thus it's safe to
575572 // construct `NonNull`.
576- unsafe { NonNull { pointer : intrinsics:: offset ( self . as_ptr ( ) , count) } }
573+ unsafe { NonNull { pointer : transmute ( intrinsics:: offset ( self . as_ptr ( ) , count) ) } }
577574 }
578575
579576 /// Calculates the offset from a pointer in bytes.
@@ -597,7 +594,7 @@ impl<T: PointeeSized> NonNull<T> {
597594 // Additionally safety contract of `offset` guarantees that the resulting pointer is
598595 // pointing to an allocation, there can't be an allocation at null, thus it's safe to
599596 // construct `NonNull`.
600- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_offset ( count) } }
597+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) . byte_offset ( count) ) } }
601598 }
602599
603600 /// Adds an offset to a pointer (convenience for `.offset(count as isize)`).
@@ -649,7 +646,7 @@ impl<T: PointeeSized> NonNull<T> {
649646 // Additionally safety contract of `offset` guarantees that the resulting pointer is
650647 // pointing to an allocation, there can't be an allocation at null, thus it's safe to
651648 // construct `NonNull`.
652- unsafe { NonNull { pointer : intrinsics:: offset ( self . as_ptr ( ) , count) } }
649+ unsafe { NonNull { pointer : transmute ( intrinsics:: offset ( self . as_ptr ( ) , count) ) } }
653650 }
654651
655652 /// Calculates the offset from a pointer in bytes (convenience for `.byte_offset(count as isize)`).
@@ -673,7 +670,7 @@ impl<T: PointeeSized> NonNull<T> {
673670 // Additionally safety contract of `add` guarantees that the resulting pointer is pointing
674671 // to an allocation, there can't be an allocation at null, thus it's safe to construct
675672 // `NonNull`.
676- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_add ( count) } }
673+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) . byte_add ( count) ) } }
677674 }
678675
679676 /// Subtracts an offset from a pointer (convenience for
@@ -755,7 +752,7 @@ impl<T: PointeeSized> NonNull<T> {
755752 // Additionally safety contract of `sub` guarantees that the resulting pointer is pointing
756753 // to an allocation, there can't be an allocation at null, thus it's safe to construct
757754 // `NonNull`.
758- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_sub ( count) } }
755+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) . byte_sub ( count) ) } }
759756 }
760757
761758 /// Calculates the distance between two pointers within the same allocation. The returned value is in
0 commit comments