Crates.io | serial_int |
lib.rs | serial_int |
version | 2.0.1 |
source | src |
created_at | 2020-12-28 17:37:09.451712 |
updated_at | 2024-05-21 19:23:23.798587 |
description | Safe, easy-to-use auto-increment integers |
homepage | |
repository | https://git.sr.ht/~jeffa/serial_int |
max_upload_size | |
id | 328350 |
size | 17,840 |
Safe, easy-to-use auto-increment integers
Serial (or auto-increment) integers make great unique identifiers because they do not need to be large (i.e. using more memory) to prevent collisions. They are always unique until they reach their max value, mimicking the behavior of PostgreSQL's SERIAL
data type. Creating serial values has minimal performance impact because it relies on simple adding rather than hashing or randomizing.
This crate provides a generator (that is also an iterator) that outputs serial values. By default, any unsigned integer from the standard library can be generated. This is essentially a counter, a simple iterator for integers. This crate is appropriately tiny.
For safety and stability, the generator "saturates" the values instead of overflowing. This guarantees that the output values are unique to that generator (except for the greatest possible value, e.g. u8::MAX or u32::MAX).
no_std
Use a generator to create unique identifiers.
let mut gen = SerialGenerator::<u32>::new();
assert_eq!(0, gen.generate());
assert_eq!(1, gen.generate());
More examples are available in the docs.
Submit a patch. If you add a new implementation of Serial, add a submodule to tests
using the provided functions. Please run the "test.sh" script before submitting a contribution.
This project is maintained on Sourcehut. Contributions through this platform are preferred. You may submit a ticket if you encounter a problem or want to request a feature. You may use the mailing list to contribute patches or public discussion.
Contributions are also welcome on the Github mirror.