Skip to content

Commit 061fc7d

Browse files
committed
Harmonize the unsync/sync get_unchecked methods
1 parent bbb7cdb commit 061fc7d

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

static-alloc/src/bump.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -585,18 +585,18 @@ impl<T> Bump<T> {
585585
);
586586

587587
let base_ptr = self.storage.get() as *mut T as *mut u8;
588-
let object = base_ptr.add(level.0);
589-
let nonnull = NonNull::new_unchecked(object).cast::<V>();
588+
let alloc = base_ptr.add(level.0);
589+
let ptr = NonNull::new_unchecked(alloc).cast::<V>();
590590

591591
debug_assert!(
592-
nonnull.as_ptr().is_aligned(),
592+
ptr.as_ptr().is_aligned(),
593593
"Tried to access an allocation with improper type"
594594
);
595595

596596
Allocation {
597-
ptr: nonnull,
598-
lifetime: AllocTime::default(),
599597
level,
598+
lifetime: AllocTime::default(),
599+
ptr,
600600
}
601601
}
602602

static-alloc/src/unsync/bump.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)