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

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(||()) };

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);

Frees the space occupied by the value at key and returns the value.

Returns

The value at key.

Examples
let mut list = unsafe { SafeFixedFreeList::<i32, 16, _>::new(||()) };
let key = list.alloc(1).unwrap();
let value = list.free(key);
assert_eq!(value, 1);

Provides immutable access to the value at key.

Returns

An immutable borrow of the value at key.

Examples
let mut list = unsafe { SafeFixedFreeList::<i32, 16, _>::new(||()) };
let key = list.alloc(1).unwrap();
let value = list.get(&key);
assert_eq!(*value, 1);

Provides mutable access to the value at key.

Returns

An immutable borrow of the value at key.

Examples
let mut list = unsafe { SafeFixedFreeList::<i32, 16, _>::new(||()) };
let mut key = list.alloc(8).unwrap();
let value = list.get_mut(&mut key);
*value = 2;
assert_eq!(list.free(key), 2);

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);

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());

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());

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.