pub struct IdBagUsize(/* private fields */);Expand description
A collection of allocatable usize integers.
Implementations§
Source§impl IdBagUsize
impl IdBagUsize
Sourcepub const fn with_inner(inner: Arc<Mutex<InnerBagUsize>>) -> Self
pub const fn with_inner(inner: Arc<Mutex<InnerBagUsize>>) -> 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::IdBagUsize;
let idbag1 = IdBagUsize::new();
let idbag2 = IdBagUsize::with_inner(idbag1.clone_inner());
let id1 = idbag1.alloc();
assert_eq!(id1.val(), 1);
let id2 = idbag2.alloc();
assert_eq!(id2.val(), 2);Sourcepub fn clone_inner(&self) -> Arc<Mutex<InnerBagUsize>>
pub fn clone_inner(&self) -> Arc<Mutex<InnerBagUsize>>
Return a clone of the internal id collection.
Sourcepub fn alloc(&self) -> IdUsize
pub fn alloc(&self) -> IdUsize
Allocate a new identifier.
use idbag::IdBagUsize;
let idbag = IdBagUsize::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.
Sourcepub fn alloc_id(&self, id: usize) -> Option<IdUsize>
pub fn alloc_id(&self, id: usize) -> Option<IdUsize>
Allocate a specific id, if available.
use idbag::IdBagUsize;
let idbag = IdBagUsize::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 IdBagUsize
impl Default for IdBagUsize
Source§fn default() -> IdBagUsize
fn default() -> IdBagUsize
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for IdBagUsize
impl !RefUnwindSafe for IdBagUsize
impl Send for IdBagUsize
impl Sync for IdBagUsize
impl Unpin for IdBagUsize
impl !UnwindSafe for IdBagUsize
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more