@@ -249,11 +249,7 @@ impl<T: ?Sized> NonNull<T> {
249
249
#[ inline]
250
250
pub const fn from_ref ( r : & T ) -> Self {
251
251
// SAFETY: A reference cannot be null.
252
- unsafe {
253
- NonNull {
254
- pointer : r as * const T ,
255
- }
256
- }
252
+ unsafe { NonNull { pointer : r as * const T } }
257
253
}
258
254
259
255
/// Converts a mutable reference to a `NonNull` pointer.
@@ -262,11 +258,7 @@ impl<T: ?Sized> NonNull<T> {
262
258
#[ inline]
263
259
pub const fn from_mut ( r : & mut T ) -> Self {
264
260
// SAFETY: A mutable reference cannot be null.
265
- unsafe {
266
- NonNull {
267
- pointer : r as * mut T ,
268
- }
269
- }
261
+ unsafe { NonNull { pointer : r as * mut T } }
270
262
}
271
263
272
264
/// Performs the same functionality as [`std::ptr::from_raw_parts`], except that a
@@ -403,7 +395,7 @@ impl<T: ?Sized> NonNull<T> {
403
395
#[ rustc_const_stable( feature = "const_nonnull_as_ref" , since = "1.73.0" ) ]
404
396
#[ must_use]
405
397
#[ inline( always) ]
406
- #[ requires( ub_checks:: can_dereference( self . as_ptr( ) as * const ( ) ) ) ] // Ensure input is convertible to a reference
398
+ #[ requires( ub_checks:: can_dereference( self . as_ptr( ) as * const ( ) ) ) ] // Ensure input is convertible to a reference
407
399
#[ ensures( |result: &&T | core:: ptr:: eq( * result, self . as_ptr( ) ) ) ] // Ensure returned reference matches pointer
408
400
pub const unsafe fn as_ref < ' a > ( & self ) -> & ' a T {
409
401
// SAFETY: the caller must guarantee that `self` meets all the
@@ -472,11 +464,7 @@ impl<T: ?Sized> NonNull<T> {
472
464
#[ inline]
473
465
pub const fn cast < U > ( self ) -> NonNull < U > {
474
466
// SAFETY: `self` is a `NonNull` pointer which is necessarily non-null
475
- unsafe {
476
- NonNull {
477
- pointer : self . as_ptr ( ) as * mut U ,
478
- }
479
- }
467
+ unsafe { NonNull { pointer : self . as_ptr ( ) as * mut U } }
480
468
}
481
469
482
470
/// Adds an offset to a pointer.
@@ -534,11 +522,7 @@ impl<T: ?Sized> NonNull<T> {
534
522
// Additionally safety contract of `offset` guarantees that the resulting pointer is
535
523
// pointing to an allocation, there can't be an allocation at null, thus it's safe to
536
524
// construct `NonNull`.
537
- unsafe {
538
- NonNull {
539
- pointer : intrinsics:: offset ( self . pointer , count) ,
540
- }
541
- }
525
+ unsafe { NonNull { pointer : intrinsics:: offset ( self . pointer , count) } }
542
526
}
543
527
544
528
/// Calculates the offset from a pointer in bytes.
@@ -562,11 +546,7 @@ impl<T: ?Sized> NonNull<T> {
562
546
// Additionally safety contract of `offset` guarantees that the resulting pointer is
563
547
// pointing to an allocation, there can't be an allocation at null, thus it's safe to
564
548
// construct `NonNull`.
565
- unsafe {
566
- NonNull {
567
- pointer : self . pointer . byte_offset ( count) ,
568
- }
569
- }
549
+ unsafe { NonNull { pointer : self . pointer . byte_offset ( count) } }
570
550
}
571
551
572
552
/// Adds an offset to a pointer (convenience for `.offset(count as isize)`).
@@ -623,11 +603,7 @@ impl<T: ?Sized> NonNull<T> {
623
603
// Additionally safety contract of `offset` guarantees that the resulting pointer is
624
604
// pointing to an allocation, there can't be an allocation at null, thus it's safe to
625
605
// construct `NonNull`.
626
- unsafe {
627
- NonNull {
628
- pointer : intrinsics:: offset ( self . pointer , count) ,
629
- }
630
- }
606
+ unsafe { NonNull { pointer : intrinsics:: offset ( self . pointer , count) } }
631
607
}
632
608
633
609
/// Calculates the offset from a pointer in bytes (convenience for `.byte_offset(count as isize)`).
@@ -651,11 +627,7 @@ impl<T: ?Sized> NonNull<T> {
651
627
// Additionally safety contract of `add` guarantees that the resulting pointer is pointing
652
628
// to an allocation, there can't be an allocation at null, thus it's safe to construct
653
629
// `NonNull`.
654
- unsafe {
655
- NonNull {
656
- pointer : self . pointer . byte_add ( count) ,
657
- }
658
- }
630
+ unsafe { NonNull { pointer : self . pointer . byte_add ( count) } }
659
631
}
660
632
661
633
/// Subtracts an offset from a pointer (convenience for
@@ -752,11 +724,7 @@ impl<T: ?Sized> NonNull<T> {
752
724
// Additionally safety contract of `sub` guarantees that the resulting pointer is pointing
753
725
// to an allocation, there can't be an allocation at null, thus it's safe to construct
754
726
// `NonNull`.
755
- unsafe {
756
- NonNull {
757
- pointer : self . pointer . byte_sub ( count) ,
758
- }
759
- }
727
+ unsafe { NonNull { pointer : self . pointer . byte_sub ( count) } }
760
728
}
761
729
762
730
/// Calculates the distance between two pointers within the same allocation. The returned value is in
@@ -1808,7 +1776,7 @@ impl<T> NonNull<[T]> {
1808
1776
#[ requires( self . len( ) . checked_mul( core:: mem:: size_of:: <T >( ) ) . is_some( ) && self . len( ) * core:: mem:: size_of:: <T >( ) <= isize :: MAX as usize ) ] // Ensure the slice size does not exceed isize::MAX
1809
1777
#[ requires( kani:: mem:: same_allocation( self . as_ptr( ) as * const ( ) , self . as_ptr( ) . byte_add( self . len( ) * core:: mem:: size_of:: <T >( ) ) as * const ( ) ) ) ] // Ensure the slice is contained within a single allocation
1810
1778
#[ ensures( |result: &&mut [ MaybeUninit <T >] | result. len( ) == self . len( ) ) ] // Length check
1811
- #[ ensures( |result: &&mut [ MaybeUninit <T >] | core:: ptr:: eq( result. as_ptr( ) , self . cast( ) . as_ptr( ) ) ) ] // Address check
1779
+ #[ ensures( |result: &&mut [ MaybeUninit <T >] | core:: ptr:: eq( result. as_ptr( ) , self . cast( ) . as_ptr( ) ) ) ] // Address check
1812
1780
pub const unsafe fn as_uninit_slice_mut < ' a > ( self ) -> & ' a mut [ MaybeUninit < T > ] {
1813
1781
// SAFETY: the caller must uphold the safety contract for `as_uninit_slice_mut`.
1814
1782
unsafe { slice:: from_raw_parts_mut ( self . cast ( ) . as_ptr ( ) , self . len ( ) ) }
@@ -1951,7 +1919,7 @@ impl<T: ?Sized> From<&T> for NonNull<T> {
1951
1919
}
1952
1920
1953
1921
#[ cfg( kani) ]
1954
- #[ unstable( feature = "kani" , issue = "none" ) ]
1922
+ #[ unstable( feature= "kani" , issue= "none" ) ]
1955
1923
mod verify {
1956
1924
use super :: * ;
1957
1925
use crate :: mem;
@@ -1996,11 +1964,7 @@ mod verify {
1996
1964
pub fn non_null_check_new ( ) {
1997
1965
let mut x: i32 = kani:: any ( ) ;
1998
1966
let xptr = & mut x;
1999
- let maybe_null_ptr = if kani:: any ( ) {
2000
- xptr as * mut i32
2001
- } else {
2002
- null_mut ( )
2003
- } ;
1967
+ let maybe_null_ptr = if kani:: any ( ) { xptr as * mut i32 } else { null_mut ( ) } ;
2004
1968
let _ = NonNull :: new ( maybe_null_ptr) ;
2005
1969
}
2006
1970
0 commit comments