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.
@@ -238,7 +235,7 @@ impl<T: PointeeSized> NonNull<T> {
238235 "NonNull::new_unchecked requires that the pointer is non-null" ,
239236 ( ptr: * mut ( ) = ptr as * mut ( ) ) => !ptr. is_null( )
240237 ) ;
241- NonNull { pointer : ptr as _ }
238+ NonNull { pointer : transmute ( ptr) }
242239 }
243240 }
244241
@@ -281,7 +278,7 @@ impl<T: PointeeSized> NonNull<T> {
281278 #[ inline]
282279 pub const fn from_ref ( r : & T ) -> Self {
283280 // SAFETY: A reference cannot be null.
284- unsafe { NonNull { pointer : r as * const T } }
281+ unsafe { NonNull { pointer : transmute ( r as * const T ) } }
285282 }
286283
287284 /// Converts a mutable reference to a `NonNull` pointer.
@@ -290,7 +287,7 @@ impl<T: PointeeSized> NonNull<T> {
290287 #[ inline]
291288 pub const fn from_mut ( r : & mut T ) -> Self {
292289 // SAFETY: A mutable reference cannot be null.
293- unsafe { NonNull { pointer : r as * mut T } }
290+ unsafe { NonNull { pointer : transmute ( r as * mut T ) } }
294291 }
295292
296293 /// Performs the same functionality as [`std::ptr::from_raw_parts`], except that a
@@ -501,7 +498,7 @@ impl<T: PointeeSized> NonNull<T> {
501498 #[ inline]
502499 pub const fn cast < U > ( self ) -> NonNull < U > {
503500 // SAFETY: `self` is a `NonNull` pointer which is necessarily non-null
504- unsafe { NonNull { pointer : self . as_ptr ( ) as * mut U } }
501+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) as * mut U ) } }
505502 }
506503
507504 /// Try to cast to a pointer of another type by checking alignment.
@@ -580,7 +577,7 @@ impl<T: PointeeSized> NonNull<T> {
580577 // Additionally safety contract of `offset` guarantees that the resulting pointer is
581578 // pointing to an allocation, there can't be an allocation at null, thus it's safe to
582579 // construct `NonNull`.
583- unsafe { NonNull { pointer : intrinsics:: offset ( self . as_ptr ( ) , count) } }
580+ unsafe { NonNull { pointer : transmute ( intrinsics:: offset ( self . as_ptr ( ) , count) ) } }
584581 }
585582
586583 /// Calculates the offset from a pointer in bytes.
@@ -604,7 +601,7 @@ impl<T: PointeeSized> NonNull<T> {
604601 // Additionally safety contract of `offset` guarantees that the resulting pointer is
605602 // pointing to an allocation, there can't be an allocation at null, thus it's safe to
606603 // construct `NonNull`.
607- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_offset ( count) } }
604+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) . byte_offset ( count) ) } }
608605 }
609606
610607 /// Adds an offset to a pointer (convenience for `.offset(count as isize)`).
@@ -656,7 +653,7 @@ impl<T: PointeeSized> NonNull<T> {
656653 // Additionally safety contract of `offset` guarantees that the resulting pointer is
657654 // pointing to an allocation, there can't be an allocation at null, thus it's safe to
658655 // construct `NonNull`.
659- unsafe { NonNull { pointer : intrinsics:: offset ( self . as_ptr ( ) , count) } }
656+ unsafe { NonNull { pointer : transmute ( intrinsics:: offset ( self . as_ptr ( ) , count) ) } }
660657 }
661658
662659 /// Calculates the offset from a pointer in bytes (convenience for `.byte_offset(count as isize)`).
@@ -680,7 +677,7 @@ impl<T: PointeeSized> NonNull<T> {
680677 // Additionally safety contract of `add` guarantees that the resulting pointer is pointing
681678 // to an allocation, there can't be an allocation at null, thus it's safe to construct
682679 // `NonNull`.
683- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_add ( count) } }
680+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) . byte_add ( count) ) } }
684681 }
685682
686683 /// Subtracts an offset from a pointer (convenience for
@@ -762,7 +759,7 @@ impl<T: PointeeSized> NonNull<T> {
762759 // Additionally safety contract of `sub` guarantees that the resulting pointer is pointing
763760 // to an allocation, there can't be an allocation at null, thus it's safe to construct
764761 // `NonNull`.
765- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_sub ( count) } }
762+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) . byte_sub ( count) ) } }
766763 }
767764
768765 /// Calculates the distance between two pointers within the same allocation. The returned value is in
0 commit comments