| Crates.io | sequential_gen |
| lib.rs | sequential_gen |
| version | 0.1.2 |
| created_at | 2024-06-26 23:53:15.516015+00 |
| updated_at | 2024-07-06 20:55:08.690216+00 |
| description | A simple sequential generator. |
| homepage | |
| repository | https://github.com/clementwanjau/sequential_gen |
| max_upload_size | |
| id | 1284984 |
| size | 16,677 |
A simple crate to generate sequential ids in Rust. This crate is useful when you need to generate sequential ids for your data structures.
I recently needed to generate sequential ids for a tree data structure that I was working on, and I couldn't find a
crate that provided this functionality with no_std capabilities. So I decided to create this crate to fill that gap.
Add the following to your Cargo.toml file:
[dependencies]
sequential_gen = "0.1"
To ensure that the generated ids are unique, we need to ensure only a single instance of the Generator trait is
available
in the program. Once you have your Generator instance, you can use the generate method to generate sequential ids.
extern crate lazy_static;
use lazy_static::lazy_static;
use sequential_gen::prelude::*;
lazy_static! {
static ref GENERATOR: SimpleGenerator<usize> = SimpleGenerator::new(1usize);
}
fn main() {
let id = GENERATOR.generate();
// Use your ID
}
Note: You can also create your own generator by implementing the
Generatortrait if you need more control over the generation process or if the provided generators do not meet your requirements.
no_std SupportThe crate is no_std compatible, but you need to disable the default features in your Cargo.toml file. You then need
to
enable the no_std feature:
[dependencies]
sequential_gen = { version = "0.1", default-features = false, features = ["no_std"] }
Note: When working in a
no_stdenvironment, you are confined to using theSimpleGeneratorstruct. In most cases this will suffice. The other generators require thestdlibrary.
The usage of the crate remains the same.
This project is licensed under the MIT license.