Struct fixed_free_list::SafeFixedFreeList
source · pub struct SafeFixedFreeList<'a, T, const N: usize, U> { /* private fields */ }
Expand description
A fixed-size free-list with key lifetime safety and macroless unique typing.
This is a somewhat experimental use of the borrowchecker,
and as such [new
] is unsafe
.
Implementations
sourceimpl<'a, T, const N: usize, U: Fn()> SafeFixedFreeList<'a, T, N, U>
impl<'a, T, const N: usize, U: Fn()> SafeFixedFreeList<'a, T, N, U>
sourcepub unsafe fn new(_: U) -> Self
pub unsafe fn new(_: U) -> Self
Creates a new empty SafeFixedFreeList
Safety
You MUST provide a unique inline closure to ensure keys are not shared with another instance.
Examples
let mut list = unsafe { SafeFixedFreeList::<i32, 16, _>::new(||()) };
sourcepub fn alloc<'k>(&mut self, value: T) -> Option<ArenaKey<'k, U>>where
'a: 'k,
pub fn alloc<'k>(&mut self, value: T) -> Option<ArenaKey<'k, U>>where
'a: 'k,
If there is space, adds value
to the free list and returns its key.
If there is no space, Drops value
and returns None
.
Returns
None
if the list was already full.
Note: value
is dropped in this case. Check [is_full
] beforehand to avoid this if desired.
Some(key)
if there was spare capacity to accommodate value
.
key
can now be used to access value
via [get
].
Examples
let mut list = unsafe { SafeFixedFreeList::<i32, 16, _>::new(||()) };
list.alloc(1);
sourcepub fn size_hint(&self) -> usize
pub fn size_hint(&self) -> usize
Returns an upper bound on the number of elements contained. The actual number of elements is guaranteed to be less than or equal to this.
Examples
let mut list = unsafe { SafeFixedFreeList::<i32, 16, _>::new(||()) };
list.alloc(5);
assert_eq!(list.size_hint(), 1);
sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Returns true
if there is no free space left.
Examples
let mut list = unsafe { SafeFixedFreeList::<i32, 1, _>::new(||()) };
assert!(!list.is_full());
list.alloc(7);
assert!(list.is_full());
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes and drops all contained values.
Time Complexity
O(1) if T: Copy
, otherwise O(N).
Examples
let mut list = unsafe { SafeFixedFreeList::<i32, 1, _>::new(||()) };
list.alloc(3);
assert!(list.is_full());
list.clear();
assert!(!list.is_full());