3 releases

0.1.2 Jan 23, 2024
0.1.1 Aug 30, 2023
0.1.0 Aug 29, 2023

#676 in Data structures

49 downloads per month
Used in fsblobstore

0BSD license

8KB
121 lines

idbag

A bag that initially contains (all) u32 values that can be allocated and returned to the bag.


lib.rs:

The IdBag is conceptually a bag that initially contains (all) u32 values. An application can request to allocate an [Id] that will contain a u32 value taken from the bag. Once the Id is dropped its u32 will automatically be returned to the bag and be available for allocation again.

Id objects are hashable and transparently maps to its internal u32. They can also be

Introductionary example

use idbag::IdBag;

// Create a new id heap.
let idbag = IdBag::new();

// Allocate an id; first id will have a value of 1.
let id = idbag.alloc();
assert_eq!(id.val(), 1);

// The id's drop handler returns 1 to the idbag
drop(id);

// Allocate another id; next id will have a value of 2.
let id = idbag.alloc();
assert_eq!(id.val(), 2);

// The id's drop handler returns 2 to the idbag
drop(id);

Dependencies

~1.8–8.5MB
~36K SLoC