@@ -370,7 +370,9 @@ impl MemBump {
370370 /// # Ok::<_, static_alloc::bump::Failure>(())
371371 /// ```
372372 ///
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.
374376 ///
375377 /// ```
376378 /// # use core::mem::MaybeUninit;
@@ -387,16 +389,28 @@ impl MemBump {
387389 /// assert_eq!(*other_val, 0); // Not UB!
388390 /// # Ok::<_, static_alloc::bump::Failure>(())
389391 /// ```
390- pub unsafe fn get_unchecked < V > ( & self , level : Level ) -> Allocation < V > {
392+ pub unsafe fn get_unchecked < V > ( & self , level : Level ) -> Allocation < ' _ , V > {
391393 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+
392400 let ptr = self . data_ptr ( ) . as_ptr ( ) ;
393401 // 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+ ) ;
395409
396410 Allocation {
397411 level,
398412 lifetime : AllocTime :: default ( ) ,
399- ptr : NonNull :: new_unchecked ( alloc ) ,
413+ ptr,
400414 }
401415 }
402416
0 commit comments