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.
@@ -94,9 +91,9 @@ impl<T: Sized> NonNull<T> {
94
91
#[ must_use]
95
92
#[ inline]
96
93
pub const fn without_provenance ( addr : NonZero < usize > ) -> Self {
97
- let pointer = crate :: ptr:: without_provenance ( addr. get ( ) ) ;
94
+ let pointer: * const T = crate :: ptr:: without_provenance ( addr. get ( ) ) ;
98
95
// SAFETY: we know `addr` is non-zero.
99
- unsafe { NonNull { pointer } }
96
+ unsafe { NonNull { pointer : transmute ( pointer ) } }
100
97
}
101
98
102
99
/// Creates a new `NonNull` that is dangling, but well-aligned.
@@ -226,7 +223,7 @@ impl<T: ?Sized> NonNull<T> {
226
223
"NonNull::new_unchecked requires that the pointer is non-null" ,
227
224
( ptr: * mut ( ) = ptr as * mut ( ) ) => !ptr. is_null( )
228
225
) ;
229
- NonNull { pointer : ptr as _ }
226
+ NonNull { pointer : transmute ( ptr) }
230
227
}
231
228
}
232
229
@@ -269,7 +266,7 @@ impl<T: ?Sized> NonNull<T> {
269
266
#[ inline]
270
267
pub const fn from_ref ( r : & T ) -> Self {
271
268
// SAFETY: A reference cannot be null.
272
- unsafe { NonNull { pointer : r as * const T } }
269
+ unsafe { NonNull { pointer : transmute ( r as * const T ) } }
273
270
}
274
271
275
272
/// Converts a mutable reference to a `NonNull` pointer.
@@ -278,7 +275,7 @@ impl<T: ?Sized> NonNull<T> {
278
275
#[ inline]
279
276
pub const fn from_mut ( r : & mut T ) -> Self {
280
277
// SAFETY: A mutable reference cannot be null.
281
- unsafe { NonNull { pointer : r as * mut T } }
278
+ unsafe { NonNull { pointer : transmute ( r as * mut T ) } }
282
279
}
283
280
284
281
/// Performs the same functionality as [`std::ptr::from_raw_parts`], except that a
@@ -489,7 +486,7 @@ impl<T: ?Sized> NonNull<T> {
489
486
#[ inline]
490
487
pub const fn cast < U > ( self ) -> NonNull < U > {
491
488
// SAFETY: `self` is a `NonNull` pointer which is necessarily non-null
492
- unsafe { NonNull { pointer : self . as_ptr ( ) as * mut U } }
489
+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) as * mut U ) } }
493
490
}
494
491
495
492
/// Try to cast to a pointer of another type by checking aligment.
@@ -568,7 +565,7 @@ impl<T: ?Sized> NonNull<T> {
568
565
// Additionally safety contract of `offset` guarantees that the resulting pointer is
569
566
// pointing to an allocation, there can't be an allocation at null, thus it's safe to
570
567
// construct `NonNull`.
571
- unsafe { NonNull { pointer : intrinsics:: offset ( self . as_ptr ( ) , count) } }
568
+ unsafe { NonNull { pointer : transmute ( intrinsics:: offset ( self . as_ptr ( ) , count) ) } }
572
569
}
573
570
574
571
/// Calculates the offset from a pointer in bytes.
@@ -592,7 +589,7 @@ impl<T: ?Sized> NonNull<T> {
592
589
// Additionally safety contract of `offset` guarantees that the resulting pointer is
593
590
// pointing to an allocation, there can't be an allocation at null, thus it's safe to
594
591
// construct `NonNull`.
595
- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_offset ( count) } }
592
+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) . byte_offset ( count) ) } }
596
593
}
597
594
598
595
/// Adds an offset to a pointer (convenience for `.offset(count as isize)`).
@@ -644,7 +641,7 @@ impl<T: ?Sized> NonNull<T> {
644
641
// Additionally safety contract of `offset` guarantees that the resulting pointer is
645
642
// pointing to an allocation, there can't be an allocation at null, thus it's safe to
646
643
// construct `NonNull`.
647
- unsafe { NonNull { pointer : intrinsics:: offset ( self . as_ptr ( ) , count) } }
644
+ unsafe { NonNull { pointer : transmute ( intrinsics:: offset ( self . as_ptr ( ) , count) ) } }
648
645
}
649
646
650
647
/// Calculates the offset from a pointer in bytes (convenience for `.byte_offset(count as isize)`).
@@ -668,7 +665,7 @@ impl<T: ?Sized> NonNull<T> {
668
665
// Additionally safety contract of `add` guarantees that the resulting pointer is pointing
669
666
// to an allocation, there can't be an allocation at null, thus it's safe to construct
670
667
// `NonNull`.
671
- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_add ( count) } }
668
+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) . byte_add ( count) ) } }
672
669
}
673
670
674
671
/// Subtracts an offset from a pointer (convenience for
@@ -750,7 +747,7 @@ impl<T: ?Sized> NonNull<T> {
750
747
// Additionally safety contract of `sub` guarantees that the resulting pointer is pointing
751
748
// to an allocation, there can't be an allocation at null, thus it's safe to construct
752
749
// `NonNull`.
753
- unsafe { NonNull { pointer : self . as_ptr ( ) . byte_sub ( count) } }
750
+ unsafe { NonNull { pointer : transmute ( self . as_ptr ( ) . byte_sub ( count) ) } }
754
751
}
755
752
756
753
/// Calculates the distance between two pointers within the same allocation. The returned value is in
0 commit comments