6 releases

0.2.2 Nov 10, 2024
0.2.1 Oct 2, 2024
0.2.0 Sep 20, 2024
0.1.2 Jan 23, 2024
0.1.1 Aug 30, 2023

#703 in Data structures

Download history 28/week @ 2024-07-29 15/week @ 2024-08-26 177/week @ 2024-09-16 41/week @ 2024-09-23 159/week @ 2024-09-30 16/week @ 2024-10-07 10/week @ 2024-10-14 51/week @ 2024-11-04 63/week @ 2024-11-11

115 downloads per month
Used in 3 crates

0BSD license

13KB
275 lines

idbag

A bag of integers that can be allocated and then returned to the bag.


lib.rs:

An id bag is conceptually a bag that initially contains (all) intger values of the type of the bag. An application can request to allocate an id from the bag, which behaves like the identifier has been taken out of the bag. Once the id is dropped its integer will automatically be returned to the bag and be available for allocation again.

Allocated identifier objects are hashable and derefernces to its internal integer value.

Introductionary example

use idbag::IdBagU32;

// Create a new id bag.
let idbag = IdBagU32::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);

Assigned integer semantics

The assigned integer increases by one each time a new id has been allocated and wraps around at its maximum value.

Dependencies

~1.1–5.5MB
~23K SLoC