1
1
#![ doc = include_str ! ( "../README.md" ) ]
2
2
#![ no_std]
3
3
#![ warn( missing_docs) ]
4
- // FIXME(11590): remove this once the lint is fixed
5
- #![ allow( unsafe_op_in_unsafe_fn) ]
6
4
7
5
use core:: fmt:: { self , Formatter , Pointer } ;
8
6
use core:: {
@@ -106,7 +104,8 @@ macro_rules! impl_ptr {
106
104
#[ inline]
107
105
pub unsafe fn byte_offset( self , count: isize ) -> Self {
108
106
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) ) } ,
110
109
PhantomData ,
111
110
)
112
111
}
@@ -126,7 +125,8 @@ macro_rules! impl_ptr {
126
125
#[ inline]
127
126
pub unsafe fn byte_add( self , count: usize ) -> Self {
128
127
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) ) } ,
130
130
PhantomData ,
131
131
)
132
132
}
@@ -176,7 +176,9 @@ impl<'a, A: IsAligned> Ptr<'a, A> {
176
176
/// for the pointee type `T`.
177
177
#[ inline]
178
178
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 }
180
182
}
181
183
182
184
/// Gets the underlying pointer, erasing the associated lifetime.
@@ -230,7 +232,9 @@ impl<'a, A: IsAligned> PtrMut<'a, A> {
230
232
/// for the pointee type `T`.
231
233
#[ inline]
232
234
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 }
234
238
}
235
239
236
240
/// Gets the underlying pointer, erasing the associated lifetime.
@@ -299,7 +303,9 @@ impl<'a, A: IsAligned> OwningPtr<'a, A> {
299
303
/// for the pointee type `T`.
300
304
#[ inline]
301
305
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 ( ) }
303
309
}
304
310
305
311
/// Consumes the [`OwningPtr`] to drop the underlying data of type `T`.
@@ -310,10 +316,11 @@ impl<'a, A: IsAligned> OwningPtr<'a, A> {
310
316
/// for the pointee type `T`.
311
317
#[ inline]
312
318
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
+ }
317
324
}
318
325
319
326
/// Gets the underlying pointer, erasing the associated lifetime.
@@ -346,7 +353,9 @@ impl<'a> OwningPtr<'a, Unaligned> {
346
353
/// # Safety
347
354
/// - `T` must be the erased pointee type for this [`OwningPtr`].
348
355
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 ( ) }
350
359
}
351
360
}
352
361
@@ -368,7 +377,9 @@ impl<'a, T> ThinSlicePtr<'a, T> {
368
377
#[ cfg( debug_assertions) ]
369
378
debug_assert ! ( index < self . len) ;
370
379
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) }
372
383
}
373
384
}
374
385
@@ -435,19 +446,22 @@ pub trait UnsafeCellDeref<'a, T>: private::SealedUnsafeCell {
435
446
impl < ' a , T > UnsafeCellDeref < ' a , T > for & ' a UnsafeCell < T > {
436
447
#[ inline]
437
448
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 ( ) }
439
451
}
440
452
#[ inline]
441
453
unsafe fn deref ( self ) -> & ' a T {
442
- & * self . get ( )
454
+ // SAFETY: The caller upholds the alias rules.
455
+ unsafe { & * self . get ( ) }
443
456
}
444
457
445
458
#[ inline]
446
459
unsafe fn read ( self ) -> T
447
460
where
448
461
T : Copy ,
449
462
{
450
- self . get ( ) . read ( )
463
+ // SAFETY: The caller upholds the alias rules.
464
+ unsafe { self . get ( ) . read ( ) }
451
465
}
452
466
}
453
467
0 commit comments