serial_int
Safe, easy-to-use auto-increment integers
Serial (or auto-increment) integers make great unique identifers 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 guarantess that the output values are unique to that generator (except for the greatest possible value, e.g. u8::MAX or u32::MAX).
Features
- Usability
- Straightforward, documented API
- Includes support for all unsigned integers in the standard library
- [?]
no_std
- [_] Serde support via feature flag
- Safety
- Panic-free
- No dependencies
- Full test coverage
- Extensibility
- Support custom serial types with single trait
- Tests use trait generics, making it easy to test new implementations
Usage
Use a generator to create unique identifiers.
let mut gen = new;
assert_eq!;
assert_eq!;
To support concurrency, simply use a wrapper. You can also use static ref
for
generators that don't have an owner.
lazy_static!
Contributing
Submit a patch. If you add a new implementation of Serial, add a submodule to
tests
using the provided functions.