Crates.io | reusable-id-pool |
lib.rs | reusable-id-pool |
version | 0.1.2 |
source | src |
created_at | 2023-12-21 07:34:25.185061 |
updated_at | 2024-05-01 07:27:12.42926 |
description | A pool for RAII IDs |
homepage | |
repository | https://github.com/davepollack/nushift/tree/master/reusable-id-pool |
max_upload_size | |
id | 1076535 |
size | 34,175 |
A pool for RAII IDs.
This crate provides two structs, ReusableIdPool
and ReusableIdPoolManual
.
use reusable_id_pool::ReusableIdPool;
let reusable_id_pool = ReusableIdPool::new();
let id = reusable_id_pool.allocate();
// Do something with the `id`, like move it into a struct. It will be returned
// to the pool when it is dropped.
A std
-only struct that hands out ArcId
s, which are opaque to the user.
To assign an ID to multiple things, use ArcId::clone(&id)
(uses Arc::clone
under the hood) to get further instances of the ID. They compare (PartialEq
) as equal.
An ID is released by dropping — when all its ArcId
s are dropped. ArcId
drop is constant time (decrementing a reference count, or appending to a free list for the final one).
A struct that hands out u64
IDs. This should be used instead of ReusableIdPool
when the ID needs to be serialised, for example over a binary ABI, as nushift-core
needs to do. The IDs must be manually returned to the pool.
#![no_std]
is supported for ReusableIdPoolManual
(set default-features = false
if this is required), but alloc
is required.
ReusableIdPool
(std
-only):
Allocate: O(1)
Release: O(1)
ReusableIdPoolManual
(std
):
Allocate: O(1)
Release: O(1)
ReusableIdPoolManual
(#![no_std]
):
Allocate: O(log n)
Release: O(log n)
The id-pool
crate has more functionality than ReusableIdPoolManual
, is always #![no_std]
, and has O(1) allocate and O(log n) release, so it probably should be used instead of ReusableIdPoolManual
for this case.