@@ -7,6 +7,7 @@ use core::{
7
7
mem:: { self , ManuallyDrop } ,
8
8
ops:: { Deref , DerefMut , Index , IndexMut } ,
9
9
ptr,
10
+ ptr:: NonNull ,
10
11
slice:: SliceIndex ,
11
12
} ;
12
13
@@ -51,7 +52,7 @@ union AlignedHeader<H, T> {
51
52
/// All of the data, like our header `OurHeaderType { a: 2 }`, the length of the vector: `2`,
52
53
/// and the contents of the vector `['x', 'z']` resides on the other side of the pointer.
53
54
pub struct HeaderVec < H , T > {
54
- ptr : * mut AlignedHeader < H , T > ,
55
+ ptr : NonNull < AlignedHeader < H , T > > ,
55
56
}
56
57
57
58
impl < H , T > HeaderVec < H , T > {
@@ -65,10 +66,10 @@ impl<H, T> HeaderVec<H, T> {
65
66
let layout = Self :: layout ( capacity) ;
66
67
let ptr = unsafe { alloc:: alloc:: alloc ( layout) } as * mut AlignedHeader < H , T > ;
67
68
68
- // Handle out-of-memory.
69
- if ptr . is_null ( ) {
69
+ let Some ( ptr ) = NonNull :: new ( ptr ) else {
70
+ // Handle out-of-memory.
70
71
alloc:: alloc:: handle_alloc_error ( layout) ;
71
- }
72
+ } ;
72
73
73
74
// Create self.
74
75
let mut this = Self { ptr } ;
@@ -173,14 +174,14 @@ impl<H, T> HeaderVec<H, T> {
173
174
/// This is useful to check if two nodes are the same. Use it with [`HeaderVec::is`].
174
175
#[ inline( always) ]
175
176
pub fn ptr ( & self ) -> * const ( ) {
176
- self . ptr as * const ( )
177
+ self . ptr . as_ptr ( ) as * const ( )
177
178
}
178
179
179
180
/// This is used to check if this is the `HeaderVec` that corresponds to the given pointer.
180
181
/// This is useful for updating weak references after [`HeaderVec::push`] returns the pointer.
181
182
#[ inline( always) ]
182
183
pub fn is ( & self , ptr : * const ( ) ) -> bool {
183
- self . ptr as * const ( ) == ptr
184
+ self . ptr . as_ptr ( ) as * const ( ) == ptr
184
185
}
185
186
186
187
/// Create a (dangerous) weak reference to the `HeaderVec`. This is useful to be able
@@ -300,19 +301,21 @@ impl<H, T> HeaderVec<H, T> {
300
301
// Reallocate the pointer.
301
302
let ptr = unsafe {
302
303
alloc:: alloc:: realloc (
303
- self . ptr as * mut u8 ,
304
+ self . ptr . as_ptr ( ) as * mut u8 ,
304
305
Self :: layout ( old_capacity) ,
305
306
Self :: elems_to_mem_bytes ( new_capacity) ,
306
307
) as * mut AlignedHeader < H , T >
307
308
} ;
308
- // Handle out-of-memory.
309
- if ptr. is_null ( ) {
309
+
310
+ let Some ( ptr) = NonNull :: new ( ptr) else {
311
+ // Handle out-of-memory.
310
312
alloc:: alloc:: handle_alloc_error ( Self :: layout ( new_capacity) ) ;
311
- }
313
+ } ;
314
+
312
315
// Check if the new pointer is different than the old one.
313
316
let previous_pointer = if ptr != self . ptr {
314
317
// Give the user the old pointer so they can update everything.
315
- Some ( self . ptr as * const ( ) )
318
+ Some ( self . ptr ( ) )
316
319
} else {
317
320
None
318
321
} ;
@@ -406,13 +409,13 @@ impl<H, T> HeaderVec<H, T> {
406
409
/// Gets the pointer to the start of the slice.
407
410
#[ inline( always) ]
408
411
fn start_ptr ( & self ) -> * const T {
409
- unsafe { ( self . ptr as * const T ) . add ( Self :: offset ( ) ) }
412
+ unsafe { ( self . ptr . as_ptr ( ) as * const T ) . add ( Self :: offset ( ) ) }
410
413
}
411
414
412
415
/// Gets the pointer to the start of the slice.
413
416
#[ inline( always) ]
414
417
fn start_ptr_mut ( & mut self ) -> * mut T {
415
- unsafe { ( self . ptr as * mut T ) . add ( Self :: offset ( ) ) }
418
+ unsafe { ( self . ptr . as_ptr ( ) as * mut T ) . add ( Self :: offset ( ) ) }
416
419
}
417
420
418
421
/// Gets the pointer to the end of the slice. This returns a mutable pointer to
@@ -425,13 +428,13 @@ impl<H, T> HeaderVec<H, T> {
425
428
#[ inline( always) ]
426
429
fn header ( & self ) -> & HeaderVecHeader < H > {
427
430
// The beginning of the memory is always the header.
428
- unsafe { & * ( self . ptr as * const HeaderVecHeader < H > ) }
431
+ unsafe { & * ( self . ptr . as_ptr ( ) as * const HeaderVecHeader < H > ) }
429
432
}
430
433
431
434
#[ inline( always) ]
432
435
fn header_mut ( & mut self ) -> & mut HeaderVecHeader < H > {
433
436
// The beginning of the memory is always the header.
434
- unsafe { & mut * ( self . ptr as * mut HeaderVecHeader < H > ) }
437
+ unsafe { & mut * ( self . ptr . as_ptr ( ) as * mut HeaderVecHeader < H > ) }
435
438
}
436
439
}
437
440
@@ -572,7 +575,7 @@ impl<H, T> Drop for HeaderVec<H, T> {
572
575
for ix in 0 ..self . len_exact ( ) {
573
576
ptr:: drop_in_place ( self . start_ptr_mut ( ) . add ( ix) ) ;
574
577
}
575
- alloc:: alloc:: dealloc ( self . ptr as * mut u8 , Self :: layout ( self . capacity ( ) ) ) ;
578
+ alloc:: alloc:: dealloc ( self . ptr . as_ptr ( ) as * mut u8 , Self :: layout ( self . capacity ( ) ) ) ;
576
579
}
577
580
}
578
581
}
0 commit comments