@@ -370,7 +370,9 @@ impl MemBump {
370
370
/// # Ok::<_, static_alloc::bump::Failure>(())
371
371
/// ```
372
372
///
373
- /// Critically, you can rely on *other* allocations to stay valid.
373
+ /// Crucially, you can rely on *other* allocations to stay valid. The caller is responsible of
374
+ /// using the returning pointer to only refer to allocations that are not referenced through
375
+ /// any other way.
374
376
///
375
377
/// ```
376
378
/// # use core::mem::MaybeUninit;
@@ -387,16 +389,28 @@ impl MemBump {
387
389
/// assert_eq!(*other_val, 0); // Not UB!
388
390
/// # Ok::<_, static_alloc::bump::Failure>(())
389
391
/// ```
390
- pub unsafe fn get_unchecked < V > ( & self , level : Level ) -> Allocation < V > {
392
+ pub unsafe fn get_unchecked < V > ( & self , level : Level ) -> Allocation < ' _ , V > {
391
393
debug_assert ! ( level. 0 < self . capacity( ) ) ;
394
+
395
+ debug_assert ! (
396
+ level <= self . level( ) ,
397
+ "Tried to access an allocation that does not yet exist"
398
+ ) ;
399
+
392
400
let ptr = self . data_ptr ( ) . as_ptr ( ) ;
393
401
// Safety: guaranteed by the caller.
394
- let alloc = ptr. offset ( level. 0 as isize ) as * mut V ;
402
+ let alloc = ptr. add ( level. 0 ) ;
403
+ let ptr = NonNull :: new_unchecked ( alloc) . cast :: < V > ( ) ;
404
+
405
+ debug_assert ! (
406
+ ptr. as_ptr( ) . is_aligned( ) ,
407
+ "Tried to access an allocation with improper type"
408
+ ) ;
395
409
396
410
Allocation {
397
411
level,
398
412
lifetime : AllocTime :: default ( ) ,
399
- ptr : NonNull :: new_unchecked ( alloc ) ,
413
+ ptr,
400
414
}
401
415
}
402
416
0 commit comments