#pool #range #id

id-pool

Create and recycle integer ids using a ranged pool

4 releases

0.2.2 Jul 6, 2022
0.2.1 Mar 19, 2021
0.2.0 Oct 29, 2020
0.1.0 Sep 17, 2020

#1837 in Data structures

Download history 225/week @ 2024-07-22 121/week @ 2024-07-29 93/week @ 2024-08-05 100/week @ 2024-08-12 127/week @ 2024-08-19 87/week @ 2024-08-26 13/week @ 2024-09-02 62/week @ 2024-09-09 89/week @ 2024-09-16 111/week @ 2024-09-23 37/week @ 2024-09-30 61/week @ 2024-10-07 100/week @ 2024-10-14 98/week @ 2024-10-21 131/week @ 2024-10-28 94/week @ 2024-11-04

423 downloads per month
Used in scalesocket

MIT license

11KB
143 lines

id-pool

Create and recycle integer ids using a ranged pool.

// 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.


lib.rs:

Create and recycle integer ids using a ranged pool.

This crate is fairly minimalistic and so 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.

Dependencies

~165KB