1
1
use crate :: cmp:: Ordering ;
2
2
use crate :: marker:: 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 ;
@@ -64,13 +64,10 @@ use crate::{fmt, hash, intrinsics, mem, ptr};
64
64
/// [null pointer optimization]: crate::option#representation
65
65
#[ stable( feature = "nonnull" , since = "1.25.0" ) ]
66
66
#[ repr( transparent) ]
67
- #[ rustc_layout_scalar_valid_range_start( 1 ) ]
68
67
#[ rustc_nonnull_optimization_guaranteed]
69
68
#[ rustc_diagnostic_item = "NonNull" ]
70
69
pub 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) ,
74
71
}
75
72
76
73
/// `NonNull` pointers are not `Send` because the data they reference may be aliased.
@@ -93,9 +90,9 @@ impl<T: Sized> NonNull<T> {
93
90
#[ must_use]
94
91
#[ inline]
95
92
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 ( ) ) ;
97
94
// SAFETY: we know `addr` is non-zero.
98
- unsafe { NonNull { pointer } }
95
+ unsafe { NonNull { pointer : transmute ( pointer ) } }
99
96
}
100
97
101
98
/// Creates a new `NonNull` that is dangling, but well-aligned.
@@ -225,7 +222,7 @@ impl<T: ?Sized> NonNull<T> {
225
222
"NonNull::new_unchecked requires that the pointer is non-null" ,
226
223
( ptr: * mut ( ) = ptr as * mut ( ) ) => !ptr. is_null( )
227
224
) ;
228
- NonNull { pointer : ptr as _ }
225
+ NonNull { pointer : transmute ( ptr) }
229
226
}
230
227
}
231
228
@@ -268,7 +265,7 @@ impl<T: ?Sized> NonNull<T> {
268
265
#[ inline]
269
266
pub const fn from_ref ( r : & T ) -> Self {
270
267
// SAFETY: A reference cannot be null.
271
- unsafe { NonNull { pointer : r as * const T } }
268
+ unsafe { NonNull { pointer : transmute ( r as * const T ) } }
272
269
}
273
270
274
271
/// Converts a mutable reference to a `NonNull` pointer.
@@ -277,7 +274,7 @@ impl<T: ?Sized> NonNull<T> {
277
274
#[ inline]
278
275
pub const fn from_mut ( r : & mut T ) -> Self {
279
276
// SAFETY: A mutable reference cannot be null.
280
- unsafe { NonNull { pointer : r as * mut T } }
277
+ unsafe { NonNull { pointer : transmute ( r as * mut T ) } }
281
278
}
282
279
283
280
/// Performs the same functionality as [`std::ptr::from_raw_parts`], except that a
@@ -488,7 +485,7 @@ impl<T: ?Sized> NonNull<T> {
488
485
#[ inline]
489
486
pub const fn cast < U > ( self ) -> NonNull < U > {
490
487
// 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 ) } }
492
489
}
493
490
494
491
/// Try to cast to a pointer of another type by checking aligment.
@@ -567,7 +564,7 @@ impl<T: ?Sized> NonNull<T> {
567
564
// Additionally safety contract of `offset` guarantees that the resulting pointer is
568
565
// pointing to an allocation, there can't be an allocation at null, thus it's safe to
569
566
// construct `NonNull`.
570
- unsafe { NonNull { pointer : intrinsics:: offset ( self . as_ptr ( ) , count) } }
567
+ unsafe { NonNull { pointer : transmute ( intrinsics:: offset ( self . as_ptr ( ) , count) ) } }
571
568
}
572
569
573
570
/// Calculates the offset from a pointer in bytes.
@@ -591,7 +588,7 @@ impl<T: ?Sized> NonNull<T> {
591
588
// Additionally safety contract of `offset` guarantees that the resulting pointer is
592
589
// pointing to an allocation, there can't be an allocation at null, thus it's safe to
593
590
// construct `NonNull`.
594
- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_offset ( count) } }
591
+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) . byte_offset ( count) ) } }
595
592
}
596
593
597
594
/// Adds an offset to a pointer (convenience for `.offset(count as isize)`).
@@ -643,7 +640,7 @@ impl<T: ?Sized> NonNull<T> {
643
640
// Additionally safety contract of `offset` guarantees that the resulting pointer is
644
641
// pointing to an allocation, there can't be an allocation at null, thus it's safe to
645
642
// construct `NonNull`.
646
- unsafe { NonNull { pointer : intrinsics:: offset ( self . as_ptr ( ) , count) } }
643
+ unsafe { NonNull { pointer : transmute ( intrinsics:: offset ( self . as_ptr ( ) , count) ) } }
647
644
}
648
645
649
646
/// Calculates the offset from a pointer in bytes (convenience for `.byte_offset(count as isize)`).
@@ -667,7 +664,7 @@ impl<T: ?Sized> NonNull<T> {
667
664
// Additionally safety contract of `add` guarantees that the resulting pointer is pointing
668
665
// to an allocation, there can't be an allocation at null, thus it's safe to construct
669
666
// `NonNull`.
670
- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_add ( count) } }
667
+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) . byte_add ( count) ) } }
671
668
}
672
669
673
670
/// Subtracts an offset from a pointer (convenience for
@@ -749,7 +746,7 @@ impl<T: ?Sized> NonNull<T> {
749
746
// Additionally safety contract of `sub` guarantees that the resulting pointer is pointing
750
747
// to an allocation, there can't be an allocation at null, thus it's safe to construct
751
748
// `NonNull`.
752
- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_sub ( count) } }
749
+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) . byte_sub ( count) ) } }
753
750
}
754
751
755
752
/// Calculates the distance between two pointers within the same allocation. The returned value is in
0 commit comments