Struct IndexPool

Source
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

Source

pub fn new() -> Self

Constructs an empty IndexPool. Indices will start at 0.

Source

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.

Source

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.

Source

pub fn request_id(&mut self, id: usize) -> Result<(), AlreadyInUse>

Attempts to allocate a specific index

Source

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.

Source

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.

Source

pub fn in_use(&self) -> usize

Returns the number of currently in-use indices

Source

pub fn is_free(&self, id: usize) -> bool

Checks if a specific index is currently free

Source

pub fn all_indices(&self) -> IndexIter<'_>

Returns an iterator over all indices which are in use

Source

pub fn all_indices_after(&self, after: usize) -> IndexAfterIter<'_>

Source

pub fn clear(&mut self)

Trait Implementations§

Source§

impl Debug for IndexPool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for IndexPool

Source§

fn default() -> Self

Constructs an empty IndexPool. Indices will start at 0.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.