pub struct Snowflake { /* private fields */ }Available on crate feature
snowflake only.Expand description
Snowflake ID generator.
Holds the worker ID and the running sequence counter.
§Example
use id_forge::snowflake::Snowflake;
let mut gen = Snowflake::new(1);
let id = gen.next_id();Implementations§
Source§impl Snowflake
impl Snowflake
Sourcepub fn new(worker_id: u16) -> Self
pub fn new(worker_id: u16) -> Self
Build a new generator with the given worker ID (0-1023) and the default epoch.
Examples found in repository?
examples/basic.rs (line 18)
7fn main() {
8 let v4 = Uuid::v4();
9 println!("UUID v4: {v4} (version={})", v4.version());
10 println!("UUID v7: {}", Uuid::v7());
11 println!("UUID nil: {}", Uuid::nil());
12
13 let a = Ulid::new();
14 let b = Ulid::new();
15 println!("ULID a: {a}");
16 println!("ULID b: {b} (monotonic: {})", b > a);
17
18 let gen = Snowflake::new(1);
19 println!("Snowflake: {}", gen.next_id());
20
21 println!("NanoID 21: {}", nanoid::generate());
22 println!("NanoID 8: {}", nanoid::with_length(8));
23
24 assert_eq!(v4, Uuid::parse_str(&v4.to_string()).unwrap());
25 assert_eq!(a, Ulid::parse_str(&a.to_string()).unwrap());
26}Sourcepub fn with_epoch(worker_id: u16, epoch_ms: u64) -> Self
pub fn with_epoch(worker_id: u16, epoch_ms: u64) -> Self
Build a new generator with a custom epoch.
Sourcepub fn next_id(&self) -> u64
pub fn next_id(&self) -> u64
Generate the next ID.
In 0.1.0 this is a placeholder. The real per-millisecond
sequence-rollover logic lands in 0.9.x.
Examples found in repository?
examples/basic.rs (line 19)
7fn main() {
8 let v4 = Uuid::v4();
9 println!("UUID v4: {v4} (version={})", v4.version());
10 println!("UUID v7: {}", Uuid::v7());
11 println!("UUID nil: {}", Uuid::nil());
12
13 let a = Ulid::new();
14 let b = Ulid::new();
15 println!("ULID a: {a}");
16 println!("ULID b: {b} (monotonic: {})", b > a);
17
18 let gen = Snowflake::new(1);
19 println!("Snowflake: {}", gen.next_id());
20
21 println!("NanoID 21: {}", nanoid::generate());
22 println!("NanoID 8: {}", nanoid::with_length(8));
23
24 assert_eq!(v4, Uuid::parse_str(&v4.to_string()).unwrap());
25 assert_eq!(a, Ulid::parse_str(&a.to_string()).unwrap());
26}Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Snowflake
impl RefUnwindSafe for Snowflake
impl Send for Snowflake
impl Sync for Snowflake
impl Unpin for Snowflake
impl UnsafeUnpin for Snowflake
impl UnwindSafe for Snowflake
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more