11use crate :: cmp:: Ordering ;
22use crate :: marker:: 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 ;
@@ -64,13 +64,10 @@ use crate::{fmt, hash, intrinsics, mem, ptr};
6464/// [null pointer optimization]: crate::option#representation
6565#[ stable( feature = "nonnull" , since = "1.25.0" ) ]
6666#[ repr( transparent) ]
67- #[ rustc_layout_scalar_valid_range_start( 1 ) ]
6867#[ rustc_nonnull_optimization_guaranteed]
6968#[ rustc_diagnostic_item = "NonNull" ]
7069pub struct NonNull < T : ?Sized > {
71- // Remember to use `.as_ptr()` instead of `.pointer`, as field projecting to
72- // this is banned by <https://github.com/rust-lang/compiler-team/issues/807>.
73- pointer : * const T ,
70+ pointer : crate :: pattern_type!( * const T is !null) ,
7471}
7572
7673/// `NonNull` pointers are not `Send` because the data they reference may be aliased.
@@ -93,9 +90,9 @@ impl<T: Sized> NonNull<T> {
9390 #[ must_use]
9491 #[ inline]
9592 pub const fn without_provenance ( addr : NonZero < usize > ) -> Self {
96- let pointer = crate :: ptr:: without_provenance ( addr. get ( ) ) ;
93+ let pointer: * const T = crate :: ptr:: without_provenance ( addr. get ( ) ) ;
9794 // SAFETY: we know `addr` is non-zero.
98- unsafe { NonNull { pointer } }
95+ unsafe { NonNull { pointer : transmute ( pointer ) } }
9996 }
10097
10198 /// Creates a new `NonNull` that is dangling, but well-aligned.
@@ -225,7 +222,7 @@ impl<T: ?Sized> NonNull<T> {
225222 "NonNull::new_unchecked requires that the pointer is non-null" ,
226223 ( ptr: * mut ( ) = ptr as * mut ( ) ) => !ptr. is_null( )
227224 ) ;
228- NonNull { pointer : ptr as _ }
225+ NonNull { pointer : transmute ( ptr) }
229226 }
230227 }
231228
@@ -268,7 +265,7 @@ impl<T: ?Sized> NonNull<T> {
268265 #[ inline]
269266 pub const fn from_ref ( r : & T ) -> Self {
270267 // SAFETY: A reference cannot be null.
271- unsafe { NonNull { pointer : r as * const T } }
268+ unsafe { NonNull { pointer : transmute ( r as * const T ) } }
272269 }
273270
274271 /// Converts a mutable reference to a `NonNull` pointer.
@@ -277,7 +274,7 @@ impl<T: ?Sized> NonNull<T> {
277274 #[ inline]
278275 pub const fn from_mut ( r : & mut T ) -> Self {
279276 // SAFETY: A mutable reference cannot be null.
280- unsafe { NonNull { pointer : r as * mut T } }
277+ unsafe { NonNull { pointer : transmute ( r as * mut T ) } }
281278 }
282279
283280 /// Performs the same functionality as [`std::ptr::from_raw_parts`], except that a
@@ -488,7 +485,7 @@ impl<T: ?Sized> NonNull<T> {
488485 #[ inline]
489486 pub const fn cast < U > ( self ) -> NonNull < U > {
490487 // SAFETY: `self` is a `NonNull` pointer which is necessarily non-null
491- unsafe { NonNull { pointer : self . as_ptr ( ) as * mut U } }
488+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) as * mut U ) } }
492489 }
493490
494491 /// Try to cast to a pointer of another type by checking aligment.
@@ -567,7 +564,7 @@ impl<T: ?Sized> NonNull<T> {
567564 // Additionally safety contract of `offset` guarantees that the resulting pointer is
568565 // pointing to an allocation, there can't be an allocation at null, thus it's safe to
569566 // construct `NonNull`.
570- unsafe { NonNull { pointer : intrinsics:: offset ( self . as_ptr ( ) , count) } }
567+ unsafe { NonNull { pointer : transmute ( intrinsics:: offset ( self . as_ptr ( ) , count) ) } }
571568 }
572569
573570 /// Calculates the offset from a pointer in bytes.
@@ -591,7 +588,7 @@ impl<T: ?Sized> NonNull<T> {
591588 // Additionally safety contract of `offset` guarantees that the resulting pointer is
592589 // pointing to an allocation, there can't be an allocation at null, thus it's safe to
593590 // construct `NonNull`.
594- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_offset ( count) } }
591+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) . byte_offset ( count) ) } }
595592 }
596593
597594 /// Adds an offset to a pointer (convenience for `.offset(count as isize)`).
@@ -643,7 +640,7 @@ impl<T: ?Sized> NonNull<T> {
643640 // Additionally safety contract of `offset` guarantees that the resulting pointer is
644641 // pointing to an allocation, there can't be an allocation at null, thus it's safe to
645642 // construct `NonNull`.
646- unsafe { NonNull { pointer : intrinsics:: offset ( self . as_ptr ( ) , count) } }
643+ unsafe { NonNull { pointer : transmute ( intrinsics:: offset ( self . as_ptr ( ) , count) ) } }
647644 }
648645
649646 /// Calculates the offset from a pointer in bytes (convenience for `.byte_offset(count as isize)`).
@@ -667,7 +664,7 @@ impl<T: ?Sized> NonNull<T> {
667664 // Additionally safety contract of `add` guarantees that the resulting pointer is pointing
668665 // to an allocation, there can't be an allocation at null, thus it's safe to construct
669666 // `NonNull`.
670- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_add ( count) } }
667+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) . byte_add ( count) ) } }
671668 }
672669
673670 /// Subtracts an offset from a pointer (convenience for
@@ -749,7 +746,7 @@ impl<T: ?Sized> NonNull<T> {
749746 // Additionally safety contract of `sub` guarantees that the resulting pointer is pointing
750747 // to an allocation, there can't be an allocation at null, thus it's safe to construct
751748 // `NonNull`.
752- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_sub ( count) } }
749+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) . byte_sub ( count) ) } }
753750 }
754751
755752 /// Calculates the distance between two pointers within the same allocation. The returned value is in
0 commit comments