@@ -95,7 +95,9 @@ impl<T: ?Sized> *const T {
95
95
#[ stable( feature = "ptr_as_ref" , since = "1.9.0" ) ]
96
96
#[ inline]
97
97
pub unsafe fn as_ref < ' a > ( self ) -> Option < & ' a T > {
98
- if self . is_null ( ) { None } else { Some ( & * self ) }
98
+ // SAFETY: the caller must guarantee that `self` is valid
99
+ // for a reference if it isn't null.
100
+ if self . is_null ( ) { None } else { unsafe { Some ( & * self ) } }
99
101
}
100
102
101
103
/// Calculates the offset from a pointer.
@@ -157,7 +159,8 @@ impl<T: ?Sized> *const T {
157
159
where
158
160
T : Sized ,
159
161
{
160
- intrinsics:: offset ( self , count)
162
+ // SAFETY: the caller must uphold the safety contract for `offset`.
163
+ unsafe { intrinsics:: offset ( self , count) }
161
164
}
162
165
163
166
/// Calculates the offset from a pointer using wrapping arithmetic.
@@ -292,7 +295,8 @@ impl<T: ?Sized> *const T {
292
295
{
293
296
let pointee_size = mem:: size_of :: < T > ( ) ;
294
297
assert ! ( 0 < pointee_size && pointee_size <= isize :: MAX as usize ) ;
295
- intrinsics:: ptr_offset_from ( self , origin)
298
+ // SAFETY: the caller must uphold the safety contract for `ptr_offset_from`.
299
+ unsafe { intrinsics:: ptr_offset_from ( self , origin) }
296
300
}
297
301
298
302
/// Returns whether two pointers are guaranteed to be equal.
@@ -471,7 +475,8 @@ impl<T: ?Sized> *const T {
471
475
where
472
476
T : Sized ,
473
477
{
474
- self . offset ( count as isize )
478
+ // SAFETY: the caller must uphold the safety contract for `offset`.
479
+ unsafe { self . offset ( count as isize ) }
475
480
}
476
481
477
482
/// Calculates the offset from a pointer (convenience for
@@ -534,7 +539,8 @@ impl<T: ?Sized> *const T {
534
539
where
535
540
T : Sized ,
536
541
{
537
- self . offset ( ( count as isize ) . wrapping_neg ( ) )
542
+ // SAFETY: the caller must uphold the safety contract for `offset`.
543
+ unsafe { self . offset ( ( count as isize ) . wrapping_neg ( ) ) }
538
544
}
539
545
540
546
/// Calculates the offset from a pointer using wrapping arithmetic.
@@ -663,7 +669,8 @@ impl<T: ?Sized> *const T {
663
669
where
664
670
T : Sized ,
665
671
{
666
- read ( self )
672
+ // SAFETY: the caller must uphold the safety contract for `read`.
673
+ unsafe { read ( self ) }
667
674
}
668
675
669
676
/// Performs a volatile read of the value from `self` without moving it. This
@@ -682,7 +689,8 @@ impl<T: ?Sized> *const T {
682
689
where
683
690
T : Sized ,
684
691
{
685
- read_volatile ( self )
692
+ // SAFETY: the caller must uphold the safety contract for `read_volatile`.
693
+ unsafe { read_volatile ( self ) }
686
694
}
687
695
688
696
/// Reads the value from `self` without moving it. This leaves the
@@ -699,7 +707,8 @@ impl<T: ?Sized> *const T {
699
707
where
700
708
T : Sized ,
701
709
{
702
- read_unaligned ( self )
710
+ // SAFETY: the caller must uphold the safety contract for `read_unaligned`.
711
+ unsafe { read_unaligned ( self ) }
703
712
}
704
713
705
714
/// Copies `count * size_of<T>` bytes from `self` to `dest`. The source
@@ -716,7 +725,8 @@ impl<T: ?Sized> *const T {
716
725
where
717
726
T : Sized ,
718
727
{
719
- copy ( self , dest, count)
728
+ // SAFETY: the caller must uphold the safety contract for `copy`.
729
+ unsafe { copy ( self , dest, count) }
720
730
}
721
731
722
732
/// Copies `count * size_of<T>` bytes from `self` to `dest`. The source
@@ -733,7 +743,8 @@ impl<T: ?Sized> *const T {
733
743
where
734
744
T : Sized ,
735
745
{
736
- copy_nonoverlapping ( self , dest, count)
746
+ // SAFETY: the caller must uphold the safety contract for `copy_nonoverlapping`.
747
+ unsafe { copy_nonoverlapping ( self , dest, count) }
737
748
}
738
749
739
750
/// Computes the offset that needs to be applied to the pointer in order to make it aligned to
0 commit comments