pub struct IdPool { /* private fields */ }
Expand description

Keeps track of free ids within a specified range, handles requests and returns of ids based on internal state.

Internally, a collection of free id ranges is stored. On a request for an id from the pool, the lowest available number will be returned to the caller. Ids can also be returned to the pool to be reused by subsequent id requests.

Examples

// initialize a new pool
let mut pool = IdPool::new();
// request some ids
assert_eq!(Some(1), pool.request_id());
assert_eq!(Some(2), pool.request_id());
assert_eq!(Some(3), pool.request_id());
// return the first id
assert_eq!(Ok(()), pool.return_id(1));
// next request returns recycled first id
assert_eq!(Some(1), pool.request_id());
// subsequent request returns the next free value
assert_eq!(Some(4), pool.request_id());

Implementations

Creates a new IdPool with a default range, which starts at 1 and ends at Num::MAX.

Creates a new IdPool with the given range.

Gets the current count of used ids.

Returns a new id or None if there are no free ids in the pool.

Returns an id to the pool or Err(Num) if the id is already in the pool.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

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 resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

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.