Struct IdBagU64

Source
pub struct IdBagU64(/* private fields */);
Expand description

A collection of allocatable u64 integers.

Implementations§

Source§

impl IdBagU64

Source

pub fn new() -> Self

Create a new id bag containing u64 integers.

Source

pub const fn with_inner(inner: Arc<Mutex<InnerBagU64>>) -> Self

Create a new id generator using a pre-existing set.

This is useful when multiple IdBag instances should share the same inner context. The practical implication of this is that the IdBag’s that share the same inner context will not yield overlapping identifier values.

use idbag::IdBagU64;

let idbag1 = IdBagU64::new();
let idbag2 = IdBagU64::with_inner(idbag1.clone_inner());

let id1 = idbag1.alloc();
assert_eq!(id1.val(), 1);

let id2 = idbag2.alloc();
assert_eq!(id2.val(), 2);
Source

pub fn clone_inner(&self) -> Arc<Mutex<InnerBagU64>>

Return a clone of the internal id collection.

Source

pub fn alloc(&self) -> IdU64

Allocate a new identifier.

use idbag::IdBagU64;

let idbag = IdBagU64::new();

let id = idbag.alloc();
assert_eq!(id.val(), 1);

let id = idbag.alloc();
assert_eq!(id.val(), 2);
§Desgin flaws

This function will hang if all the id’s have been exhausted.

Source

pub fn alloc_id(&self, id: u64) -> Option<IdU64>

Allocate a specific id, if available.

use idbag::IdBagU64;

let idbag = IdBagU64::new();

let Some(id) = idbag.alloc_id(42) else {
  panic!("Unexpectedly unable to allocate id 42");
};
assert_eq!(id.val(), 42);

let id = idbag.alloc_id(42);
assert_eq!(id, None);

Trait Implementations§

Source§

impl Default for IdBagU64

Source§

fn default() -> IdBagU64

Returns the “default value” for a type. Read more

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.