Skip to main content

SnowflakeIdGenerator

Struct SnowflakeIdGenerator 

Source
pub struct SnowflakeIdGenerator { /* private fields */ }
Expand description

A Snowflake ID generator with a configurable BitLayout.

§Thread safety

SnowflakeIdGenerator is not Send + Sync by itself because its internal state is mutated on every call. Wrap it in a Mutex / RwLock when sharing across threads.

§Example — default layout

use snowflake_gen::SnowflakeIdGenerator;

let mut idgen = SnowflakeIdGenerator::new(1, 1).unwrap();
let id = idgen.generate().unwrap();
assert!(id > 0);

Implementations§

Source§

impl SnowflakeIdGenerator

Source

pub fn new(machine_id: i64, node_id: i64) -> Result<Self, SnowflakeError>

Constructs a generator using the UNIX epoch and the default BitLayout.

use snowflake_gen::SnowflakeIdGenerator;

let mut idgen = SnowflakeIdGenerator::new(1, 1).unwrap();
Source

pub fn with_epoch( machine_id: i64, node_id: i64, epoch: SystemTime, ) -> Result<Self, SnowflakeError>

Constructs a generator using a custom epoch and the default BitLayout.

use std::time::{Duration, UNIX_EPOCH};
use snowflake_gen::SnowflakeIdGenerator;

// Discord epoch: 1 January 2015 00:00:00 UTC
let discord_epoch = UNIX_EPOCH + Duration::from_millis(1_420_070_400_000);
let mut idgen = SnowflakeIdGenerator::with_epoch(1, 1, discord_epoch).unwrap();
Source

pub fn with_layout( machine_id: i64, node_id: i64, layout: BitLayout, ) -> Result<Self, SnowflakeError>

Constructs a generator with a fully custom BitLayout and UNIX epoch.

use snowflake_gen::{BitLayout, SnowflakeIdGenerator};

let layout = BitLayout::new(38, 8, 7, 10).unwrap();
let mut idgen = SnowflakeIdGenerator::with_layout(1, 1, layout).unwrap();
Source

pub fn with_layout_and_epoch( machine_id: i64, node_id: i64, layout: BitLayout, epoch: SystemTime, ) -> Result<Self, SnowflakeError>

Constructs a generator with a fully custom BitLayout and epoch.

§Errors
Source§

impl SnowflakeIdGenerator

Source

pub fn generate(&mut self) -> Result<i64, SnowflakeError>

Generates the next ID, blocking until the next millisecond if the sequence counter wraps.

use snowflake_gen::SnowflakeIdGenerator;

let mut idgen = SnowflakeIdGenerator::new(1, 1).unwrap();
let id = idgen.generate().unwrap();
assert!(id > 0);
Source

pub fn real_time_generate(&mut self) -> Result<i64, SnowflakeError>

Generates the next ID, always reading the current clock.

Source

pub fn lazy_generate(&mut self) -> i64

Generates the next ID without reading the system clock.

Instead of a syscall, lazy_generate synthetically increments last_time_millis whenever the sequence counter wraps. This makes it the fastest generation mode but comes with an important constraint:

Do not mix lazy_generate with generate or real_time_generate on the same generator instance. Because lazy_generate can advance last_time_millis ahead of the real clock, a subsequent clock-based call may produce a timestamp that lazy_generate already used, resulting in duplicate IDs.

SnowflakeIdBucket uses lazy_generate internally on a dedicated generator that is never exposed for clock-based calls, so it is safe.

Source§

impl SnowflakeIdGenerator

Source

pub fn decompose(&self, id: i64) -> SnowflakeComponents

Decodes a previously generated ID back into its constituent fields.

use snowflake_gen::SnowflakeIdGenerator;

let mut idgen = SnowflakeIdGenerator::new(3, 7).unwrap();
let id = idgen.generate().unwrap();
let parts = idgen.decompose(id);

assert_eq!(parts.machine_id, 3);
assert_eq!(parts.node_id, 7);
Source§

impl SnowflakeIdGenerator

Source

pub fn layout(&self) -> &BitLayout

Returns a reference to the BitLayout in use.

Source

pub fn epoch(&self) -> SystemTime

Returns the epoch this generator was constructed with.

Source

pub fn machine_id(&self) -> i64

Returns the machine identifier.

Source

pub fn node_id(&self) -> i64

Returns the node (worker) identifier.

Trait Implementations§

Source§

impl Clone for SnowflakeIdGenerator

Source§

fn clone(&self) -> SnowflakeIdGenerator

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SnowflakeIdGenerator

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Copy for SnowflakeIdGenerator

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.