Skip to main content

Snowflake

Struct Snowflake 

Source
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

Source

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}
Source

pub fn with_epoch(worker_id: u16, epoch_ms: u64) -> Self

Build a new generator with a custom epoch.

Source

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§

Source§

impl Debug for Snowflake

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.