Struct id_pool::IdPool [−][src]
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
impl IdPool
[src]
pub fn new() -> Self
[src]
Creates a new IdPool
with a default range, which
starts at 1
and ends at Num::max_value()
.
pub fn new_ranged(range: Range<usize>) -> Self
[src]
Creates a new IdPool
with the given range.
pub fn used_count(&self) -> usize
[src]
Gets the current count of used ids.
pub fn request_id(&mut self) -> Option<usize>
[src]
Returns a new id or None
if there are no free ids
in the pool.
pub fn return_id(&mut self, id: usize) -> Result<(), usize>
[src]
Returns an id to the pool or Err(Num)
if the
id is already in the pool.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for IdPool
impl Send for IdPool
impl Sync for IdPool
impl Unpin for IdPool
impl UnwindSafe for IdPool
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,