SnowflakeBuilder

Struct SnowflakeBuilder 

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

A builder for creating a Snowflake generator with custom configuration.

Implementations§

Source§

impl SnowflakeBuilder

Source

pub fn with_worker_id(self, worker_id: u64) -> Self

Set the worker ID for the Snowflake generator.

Examples found in repository?
examples/custom_config.rs (line 10)
3fn main() -> Result<(), Box<dyn Error>> {
4    let worker_id = 1;
5    let worker_id_bits = 4;
6    let epoch: u64 = 1609459200000; // 2021-01-01 00:00:00.000 UTC
7
8    let mut snowflake = Snowflake::builder()
9        .with_worker_id_bits(worker_id_bits)
10        .with_worker_id(worker_id)
11        .with_epoch(epoch)
12        .build()?;
13
14    let sfid = snowflake.generate()?;
15    println!("Snowflake ID: {}", sfid);
16    Ok(())
17}
Source

pub fn with_worker_id_bits(self, worker_id_bits: u64) -> Self

Set the number of bits used for the worker ID.

Examples found in repository?
examples/custom_config.rs (line 9)
3fn main() -> Result<(), Box<dyn Error>> {
4    let worker_id = 1;
5    let worker_id_bits = 4;
6    let epoch: u64 = 1609459200000; // 2021-01-01 00:00:00.000 UTC
7
8    let mut snowflake = Snowflake::builder()
9        .with_worker_id_bits(worker_id_bits)
10        .with_worker_id(worker_id)
11        .with_epoch(epoch)
12        .build()?;
13
14    let sfid = snowflake.generate()?;
15    println!("Snowflake ID: {}", sfid);
16    Ok(())
17}
Source

pub fn with_timeout_millis(self, timeout_millis: u128) -> Self

Set the timeout duration for waiting for the next time period.

Source

pub fn with_epoch(self, epoch: u64) -> Self

Set the epoch time.

Examples found in repository?
examples/custom_config.rs (line 11)
3fn main() -> Result<(), Box<dyn Error>> {
4    let worker_id = 1;
5    let worker_id_bits = 4;
6    let epoch: u64 = 1609459200000; // 2021-01-01 00:00:00.000 UTC
7
8    let mut snowflake = Snowflake::builder()
9        .with_worker_id_bits(worker_id_bits)
10        .with_worker_id(worker_id)
11        .with_epoch(epoch)
12        .build()?;
13
14    let sfid = snowflake.generate()?;
15    println!("Snowflake ID: {}", sfid);
16    Ok(())
17}
Source

pub fn build(self) -> Result<Snowflake, SnowflakeError>

Build the Snowflake generator with the specified configuration.

Examples found in repository?
examples/custom_config.rs (line 12)
3fn main() -> Result<(), Box<dyn Error>> {
4    let worker_id = 1;
5    let worker_id_bits = 4;
6    let epoch: u64 = 1609459200000; // 2021-01-01 00:00:00.000 UTC
7
8    let mut snowflake = Snowflake::builder()
9        .with_worker_id_bits(worker_id_bits)
10        .with_worker_id(worker_id)
11        .with_epoch(epoch)
12        .build()?;
13
14    let sfid = snowflake.generate()?;
15    println!("Snowflake ID: {}", sfid);
16    Ok(())
17}

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.