pub struct IndexPool { /* private fields */ }Expand description
A pool which manages allocation of unique indices. Acts like a psuedo-memory allocator.
Implementations§
Source§impl IndexPool
impl IndexPool
Sourcepub fn with_initial_index(index: usize) -> Self
pub fn with_initial_index(index: usize) -> Self
Constructs an empty IndexPool. index will be the first
index returned from new_id. You can logically think of
this as either specifying a base index for the pool, or
pre-allocating the [0..index) range. This datastructure
does not care which is your usecase, and neither has any
kind of performance penalty, except that in_use() will
include the [0..index) range.
Sourcepub fn new_id(&mut self) -> usize
pub fn new_id(&mut self) -> usize
Allocates a new index for use. This is guaranteed to not be any index
which has previously been returned from new_id but has not yet been
passed to return_id.
Sourcepub fn request_id(&mut self, id: usize) -> Result<(), AlreadyInUse>
pub fn request_id(&mut self, id: usize) -> Result<(), AlreadyInUse>
Attempts to allocate a specific index
Sourcepub fn return_id(&mut self, id: usize) -> Result<(), AlreadyReturned>
pub fn return_id(&mut self, id: usize) -> Result<(), AlreadyReturned>
Gives an Id back to the pool so that it may be handed out again. Returns Err if the Id was not in use at the time. Whether ignoring such an error is okay is up to your own usecase.
Sourcepub fn maximum(&self) -> usize
pub fn maximum(&self) -> usize
Returns an upper bound on the number of IDs which have been
allocated, specifically the highest numbered ID in use + 1.
Useful if you’re going to e.g. create a Vec which has room
for all of your IDs.
Sourcepub fn all_indices(&self) -> IndexIter<'_> ⓘ
pub fn all_indices(&self) -> IndexIter<'_> ⓘ
Returns an iterator over all indices which are in use