1
1
use crate :: cmp:: Ordering ;
2
2
use crate :: marker:: { PointeeSized , Unsize } ;
3
- use crate :: mem:: { MaybeUninit , SizedTypeProperties } ;
3
+ use crate :: mem:: { MaybeUninit , SizedTypeProperties , transmute } ;
4
4
use crate :: num:: NonZero ;
5
5
use crate :: ops:: { CoerceUnsized , DispatchFromDyn } ;
6
6
use crate :: pin:: PinCoerceUnsized ;
@@ -69,13 +69,10 @@ use crate::{fmt, hash, intrinsics, mem, ptr};
69
69
/// [null pointer optimization]: crate::option#representation
70
70
#[ stable( feature = "nonnull" , since = "1.25.0" ) ]
71
71
#[ repr( transparent) ]
72
- #[ rustc_layout_scalar_valid_range_start( 1 ) ]
73
72
#[ rustc_nonnull_optimization_guaranteed]
74
73
#[ rustc_diagnostic_item = "NonNull" ]
75
74
pub 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) ,
79
76
}
80
77
81
78
/// `NonNull` pointers are not `Send` because the data they reference may be aliased.
@@ -99,9 +96,9 @@ impl<T: Sized> NonNull<T> {
99
96
#[ must_use]
100
97
#[ inline]
101
98
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 ( ) ) ;
103
100
// SAFETY: we know `addr` is non-zero.
104
- unsafe { NonNull { pointer } }
101
+ unsafe { NonNull { pointer : transmute ( pointer ) } }
105
102
}
106
103
107
104
/// Creates a new `NonNull` that is dangling, but well-aligned.
@@ -231,7 +228,7 @@ impl<T: PointeeSized> NonNull<T> {
231
228
"NonNull::new_unchecked requires that the pointer is non-null" ,
232
229
( ptr: * mut ( ) = ptr as * mut ( ) ) => !ptr. is_null( )
233
230
) ;
234
- NonNull { pointer : ptr as _ }
231
+ NonNull { pointer : transmute ( ptr) }
235
232
}
236
233
}
237
234
@@ -274,7 +271,7 @@ impl<T: PointeeSized> NonNull<T> {
274
271
#[ inline]
275
272
pub const fn from_ref ( r : & T ) -> Self {
276
273
// SAFETY: A reference cannot be null.
277
- unsafe { NonNull { pointer : r as * const T } }
274
+ unsafe { NonNull { pointer : transmute ( r as * const T ) } }
278
275
}
279
276
280
277
/// Converts a mutable reference to a `NonNull` pointer.
@@ -283,7 +280,7 @@ impl<T: PointeeSized> NonNull<T> {
283
280
#[ inline]
284
281
pub const fn from_mut ( r : & mut T ) -> Self {
285
282
// SAFETY: A mutable reference cannot be null.
286
- unsafe { NonNull { pointer : r as * mut T } }
283
+ unsafe { NonNull { pointer : transmute ( r as * mut T ) } }
287
284
}
288
285
289
286
/// Performs the same functionality as [`std::ptr::from_raw_parts`], except that a
@@ -494,7 +491,7 @@ impl<T: PointeeSized> NonNull<T> {
494
491
#[ inline]
495
492
pub const fn cast < U > ( self ) -> NonNull < U > {
496
493
// 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 ) } }
498
495
}
499
496
500
497
/// Try to cast to a pointer of another type by checking alignment.
@@ -573,7 +570,7 @@ impl<T: PointeeSized> NonNull<T> {
573
570
// Additionally safety contract of `offset` guarantees that the resulting pointer is
574
571
// pointing to an allocation, there can't be an allocation at null, thus it's safe to
575
572
// construct `NonNull`.
576
- unsafe { NonNull { pointer : intrinsics:: offset ( self . as_ptr ( ) , count) } }
573
+ unsafe { NonNull { pointer : transmute ( intrinsics:: offset ( self . as_ptr ( ) , count) ) } }
577
574
}
578
575
579
576
/// Calculates the offset from a pointer in bytes.
@@ -597,7 +594,7 @@ impl<T: PointeeSized> NonNull<T> {
597
594
// Additionally safety contract of `offset` guarantees that the resulting pointer is
598
595
// pointing to an allocation, there can't be an allocation at null, thus it's safe to
599
596
// construct `NonNull`.
600
- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_offset ( count) } }
597
+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) . byte_offset ( count) ) } }
601
598
}
602
599
603
600
/// Adds an offset to a pointer (convenience for `.offset(count as isize)`).
@@ -649,7 +646,7 @@ impl<T: PointeeSized> NonNull<T> {
649
646
// Additionally safety contract of `offset` guarantees that the resulting pointer is
650
647
// pointing to an allocation, there can't be an allocation at null, thus it's safe to
651
648
// construct `NonNull`.
652
- unsafe { NonNull { pointer : intrinsics:: offset ( self . as_ptr ( ) , count) } }
649
+ unsafe { NonNull { pointer : transmute ( intrinsics:: offset ( self . as_ptr ( ) , count) ) } }
653
650
}
654
651
655
652
/// Calculates the offset from a pointer in bytes (convenience for `.byte_offset(count as isize)`).
@@ -673,7 +670,7 @@ impl<T: PointeeSized> NonNull<T> {
673
670
// Additionally safety contract of `add` guarantees that the resulting pointer is pointing
674
671
// to an allocation, there can't be an allocation at null, thus it's safe to construct
675
672
// `NonNull`.
676
- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_add ( count) } }
673
+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) . byte_add ( count) ) } }
677
674
}
678
675
679
676
/// Subtracts an offset from a pointer (convenience for
@@ -755,7 +752,7 @@ impl<T: PointeeSized> NonNull<T> {
755
752
// Additionally safety contract of `sub` guarantees that the resulting pointer is pointing
756
753
// to an allocation, there can't be an allocation at null, thus it's safe to construct
757
754
// `NonNull`.
758
- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_sub ( count) } }
755
+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) . byte_sub ( count) ) } }
759
756
}
760
757
761
758
/// Calculates the distance between two pointers within the same allocation. The returned value is in
0 commit comments