pub struct Allocation<'a, T: ?Sized = u8> {
pub ptr: NonNull<T>,
pub lifetime: AllocTime<'a>,
pub level: Level,
}Expand description
A successful allocation and current Level.
§Design notes
The ptr field is, when returned by an allocator, always a valid pointer. However, we can not
express this as a mutable reference to MaybeUninit for unsized types. Instead, a pointer is
needed to provide address, provenance, and pointer metadata. The cost of this is that we discard
the validity information which has to be unsafely re-applied by the user (of course, verifying
that the point actually is valid since this type is fully public).
Fields§
§ptr: NonNull<T>Pointer to the uninitialized region with specified layout.
lifetime: AllocTime<'a>The lifetime of the allocation.
level: LevelThe observed amount of consumed bytes after the allocation.
For Bump this is precise, i.e. the offset of the ptr in the arena data is the level’s
byte offset minus the layout size.
Implementations§
Source§impl<T> Allocation<'_, T>
impl<T> Allocation<'_, T>
Sourcepub fn level_start(&self) -> Level
pub fn level_start(&self) -> Level
Get the Level at which the allocation starts.
This can be used as the level of Bump::get_unchecked with the arena it as allocated
from. There is an equivalent method for some unsized types.
§Usage
use static_alloc::Bump;
let arena = Bump::<[u32; 4]>::uninit();
let alloc = arena.get::<u32>().unwrap();
assert_eq!(alloc.level.byte_offset(), 4);
assert_eq!(alloc.level_start().byte_offset(), 0);Source§impl<T> Allocation<'_, [T]>
impl<T> Allocation<'_, [T]>
Sourcepub fn level_start(&self) -> Level
pub fn level_start(&self) -> Level
Get the Level at which the allocation starts.
This can be used as the level of Bump::get_unchecked with the arena it as allocated
from. There is an equivalent method for some unsized types.
See the sized Allocation::level_start.
Source§impl<'alloc, T> Allocation<'alloc, T>
impl<'alloc, T> Allocation<'alloc, T>
Sourcepub unsafe fn leak(self, val: T) -> &'alloc mut T
pub unsafe fn leak(self, val: T) -> &'alloc mut T
Write a value into the allocation and leak it.
§Safety
Must have been allocated for a layout that fits the layout of T previously. The pointer must not be aliased.
§Usage
Consider the alternative Bump::leak to safely allocate and directly leak a value.
Sourcepub unsafe fn boxed(self, val: T) -> LeakBox<'alloc, T>
pub unsafe fn boxed(self, val: T) -> LeakBox<'alloc, T>
Write a value into the allocation and own it.
§Safety
Must have been allocated for a layout that fits the layout of T previously. The pointer must not be aliased.
§Usage
Consider the alternative Bump::leak to safely allocate and directly leak a value.
Sourcepub unsafe fn uninit(self) -> &'alloc mut MaybeUninit<T>
pub unsafe fn uninit(self) -> &'alloc mut MaybeUninit<T>
Convert this into a mutable reference to an uninitialized slot.
§Safety
Must have been allocated for a layout that fits the layout of T previously.