Crates.io | sarmio |
lib.rs | sarmio |
version | 0.1.1 |
source | src |
created_at | 2021-01-08 23:41:24.975484 |
updated_at | 2021-01-09 23:51:20.856584 |
description | Distributed unique ID generator. |
homepage | |
repository | https://github.com/ycd/sarmio |
max_upload_size | |
id | 335184 |
size | 5,190 |
Sarmio creates a unique ID that is between the range of 0 > n > (2 ^ 64) -1
, also known as unsigned 64 bit integer.
First of all, add sarmio
as a dependency to your cargo.toml
.
[dependencies]
sarmio = "0.1"
fn main() {
// Create new Sarmio instance with a machine-id of 255.
let mut sarmio_one = sarmio::Sarmio::new(255);
let mut sarmio_two = sarmio::Sarmio::new(555);
// Sarmio implements Iterator
// Which means you can iterate over it to create new IDs.
let id1 = match sarmio_one.next_id() {
Some(s) => s,
None => 0,
};
let id2 = match sarmio_two.next_id() {
Some(s) => s,
None => 0,
};
// Or create a new with next_id() syntax.
// Decompose it, get the values like
// Unix time in that moment, machine id
// and the Unique ID.
let id1_decomposed = sarmio::decompose(id1);
let id2_decomposed = sarmio::decompose(id2);
println!("{:?}", id1_decomposed); // ID { id: 27015264398737663, machine_id: 255, time: 1610235238 }
println!("{:?}", id2_decomposed); // ID { id: 27015264398737963, machine_id: 555, time: 1610235238 }
// Check which ID is older.
let is_older = id2_decomposed.older(&id1_decomposed);
println!("{:?}", is_older); // false
// Check whether the ID's are created in the same machine.
let same_machine = id2_decomposed.same_machine(&id1_decomposed);
println!("{:?}", same_machine) // false
}