https://github.com/adamsky/id-pool
Create and recycle integer ids using a ranged pool
https://github.com/adamsky/id-pool
id integer pool range
Last synced: 5 months ago
JSON representation
Create and recycle integer ids using a ranged pool
- Host: GitHub
- URL: https://github.com/adamsky/id-pool
- Owner: adamsky
- License: mit
- Created: 2020-07-20T18:11:49.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-07-06T20:15:56.000Z (over 3 years ago)
- Last Synced: 2025-04-01T11:11:22.378Z (9 months ago)
- Topics: id, integer, pool, range
- Language: Rust
- Homepage:
- Size: 13.7 KB
- Stars: 5
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# id-pool
Create and recycle integer ids using a ranged pool.
```rust
// create a new id pool with an available range
let mut pool = IdPool::new_ranged(1..10);
// request ids from the pool
let id1 = pool.request_id()); // 1
let id2 = pool.request_id()); // 2
let id3 = pool.request_id()); // 3
// return arbitrary id back into the pool
pool.return_id(2)?;
// recycle the returned id during subsequent request
let id4 = pool.request_id()); // 2
let id5 = pool.request_id()); // 4
```
This crate is fairly minimalistic and so currently only
deals with single type of numerical ids. These can be
either `usize` (default), `u64`, `u32` or `u16`, chosen
with the use of appropriate crate feature.
The main exported structure `IdPool` can be
initialized with a custom range and then queried for
new ids that are contained within that range. During
the course of the program, ids can be returned to the
pool to be reused for subsequent id request calls.