11#![ doc = include_str ! ( "../README.md" ) ]
22#![ no_std]
33#![ warn( missing_docs) ]
4- // FIXME(11590): remove this once the lint is fixed
5- #![ allow( unsafe_op_in_unsafe_fn) ]
64
75use core:: fmt:: { self , Formatter , Pointer } ;
86use core:: {
@@ -106,7 +104,8 @@ macro_rules! impl_ptr {
106104 #[ inline]
107105 pub unsafe fn byte_offset( self , count: isize ) -> Self {
108106 Self (
109- NonNull :: new_unchecked( self . as_ptr( ) . offset( count) ) ,
107+ // SAFETY: The caller upholds safety for `offset` and ensures the result is not null.
108+ unsafe { NonNull :: new_unchecked( self . as_ptr( ) . offset( count) ) } ,
110109 PhantomData ,
111110 )
112111 }
@@ -126,7 +125,8 @@ macro_rules! impl_ptr {
126125 #[ inline]
127126 pub unsafe fn byte_add( self , count: usize ) -> Self {
128127 Self (
129- NonNull :: new_unchecked( self . as_ptr( ) . add( count) ) ,
128+ // SAFETY: The caller upholds safety for `add` and ensures the result is not null.
129+ unsafe { NonNull :: new_unchecked( self . as_ptr( ) . add( count) ) } ,
130130 PhantomData ,
131131 )
132132 }
@@ -176,7 +176,9 @@ impl<'a, A: IsAligned> Ptr<'a, A> {
176176 /// for the pointee type `T`.
177177 #[ inline]
178178 pub unsafe fn deref < T > ( self ) -> & ' a T {
179- & * self . as_ptr ( ) . cast :: < T > ( ) . debug_ensure_aligned ( )
179+ let ptr = self . as_ptr ( ) . cast :: < T > ( ) . debug_ensure_aligned ( ) ;
180+ // SAFETY: The caller ensures the pointee is of type `T` and the pointer can be dereferenced.
181+ unsafe { & * ptr }
180182 }
181183
182184 /// Gets the underlying pointer, erasing the associated lifetime.
@@ -230,7 +232,9 @@ impl<'a, A: IsAligned> PtrMut<'a, A> {
230232 /// for the pointee type `T`.
231233 #[ inline]
232234 pub unsafe fn deref_mut < T > ( self ) -> & ' a mut T {
233- & mut * self . as_ptr ( ) . cast :: < T > ( ) . debug_ensure_aligned ( )
235+ let ptr = self . as_ptr ( ) . cast :: < T > ( ) . debug_ensure_aligned ( ) ;
236+ // SAFETY: The caller ensures the pointee is of type `T` and the pointer can be dereferenced.
237+ unsafe { & mut * ptr }
234238 }
235239
236240 /// Gets the underlying pointer, erasing the associated lifetime.
@@ -299,7 +303,9 @@ impl<'a, A: IsAligned> OwningPtr<'a, A> {
299303 /// for the pointee type `T`.
300304 #[ inline]
301305 pub unsafe fn read < T > ( self ) -> T {
302- self . as_ptr ( ) . cast :: < T > ( ) . debug_ensure_aligned ( ) . read ( )
306+ let ptr = self . as_ptr ( ) . cast :: < T > ( ) . debug_ensure_aligned ( ) ;
307+ // SAFETY: The caller ensure the pointee is of type `T` and uphold safety for `read`.
308+ unsafe { ptr. read ( ) }
303309 }
304310
305311 /// Consumes the [`OwningPtr`] to drop the underlying data of type `T`.
@@ -310,10 +316,11 @@ impl<'a, A: IsAligned> OwningPtr<'a, A> {
310316 /// for the pointee type `T`.
311317 #[ inline]
312318 pub unsafe fn drop_as < T > ( self ) {
313- self . as_ptr ( )
314- . cast :: < T > ( )
315- . debug_ensure_aligned ( )
316- . drop_in_place ( ) ;
319+ let ptr = self . as_ptr ( ) . cast :: < T > ( ) . debug_ensure_aligned ( ) ;
320+ // SAFETY: The caller ensure the pointee is of type `T` and uphold safety for `drop_in_place`.
321+ unsafe {
322+ ptr. drop_in_place ( ) ;
323+ }
317324 }
318325
319326 /// Gets the underlying pointer, erasing the associated lifetime.
@@ -346,7 +353,9 @@ impl<'a> OwningPtr<'a, Unaligned> {
346353 /// # Safety
347354 /// - `T` must be the erased pointee type for this [`OwningPtr`].
348355 pub unsafe fn read_unaligned < T > ( self ) -> T {
349- self . as_ptr ( ) . cast :: < T > ( ) . read_unaligned ( )
356+ let ptr = self . as_ptr ( ) . cast :: < T > ( ) ;
357+ // SAFETY: The caller ensure the pointee is of type `T` and uphold safety for `read_unaligned`.
358+ unsafe { ptr. read_unaligned ( ) }
350359 }
351360}
352361
@@ -368,7 +377,9 @@ impl<'a, T> ThinSlicePtr<'a, T> {
368377 #[ cfg( debug_assertions) ]
369378 debug_assert ! ( index < self . len) ;
370379
371- & * self . ptr . as_ptr ( ) . add ( index)
380+ let ptr = self . ptr . as_ptr ( ) ;
381+ // SAFETY: `index` is in-bounds so the resulting pointer is valid to dereference.
382+ unsafe { & * ptr. add ( index) }
372383 }
373384}
374385
@@ -435,19 +446,22 @@ pub trait UnsafeCellDeref<'a, T>: private::SealedUnsafeCell {
435446impl < ' a , T > UnsafeCellDeref < ' a , T > for & ' a UnsafeCell < T > {
436447 #[ inline]
437448 unsafe fn deref_mut ( self ) -> & ' a mut T {
438- & mut * self . get ( )
449+ // SAFETY: The caller upholds the alias rules.
450+ unsafe { & mut * self . get ( ) }
439451 }
440452 #[ inline]
441453 unsafe fn deref ( self ) -> & ' a T {
442- & * self . get ( )
454+ // SAFETY: The caller upholds the alias rules.
455+ unsafe { & * self . get ( ) }
443456 }
444457
445458 #[ inline]
446459 unsafe fn read ( self ) -> T
447460 where
448461 T : Copy ,
449462 {
450- self . get ( ) . read ( )
463+ // SAFETY: The caller upholds the alias rules.
464+ unsafe { self . get ( ) . read ( ) }
451465 }
452466}
453467
0 commit comments