Struct SnowflakeGenerator

Source
pub struct SnowflakeGenerator {
    pub last_snowflake: Snowflake,
}
Expand description

A snowflake generator that can be used to generate snowflake IDs.

Fields§

§last_snowflake: Snowflake

The last snowflake ID generated by the generator.

Implementations§

Source§

impl SnowflakeGenerator

Source

pub fn new(worker_id: u64) -> SnowflakeGenerator

Create a new snowflake generator with the given worker ID

§Arguments
  • worker_id - The worker ID of the snowflake generator
§Returns

A new SnowflakeGenerator

§Example
use rusty_snowflake::{SnowflakeGenerator, Snowflake};

const WORKER_ID: u64 = 420;

let generator = SnowflakeGenerator::new(WORKER_ID);

assert_eq!(generator.last_snowflake, Snowflake::new(WORKER_ID));
Source

pub fn start_at(snowflake: &Snowflake) -> SnowflakeGenerator

Start at the given snowflake

§Arguments
  • snowflake - The snowflake to start at
§Returns

A SnowflakeGenerator

§Example
use rusty_snowflake::{Snowflake, SnowflakeGenerator};

const WORKER_ID: u64 = 420;

let generator = SnowflakeGenerator::start_at(&Snowflake::new(WORKER_ID));

assert_eq!(generator.last_snowflake, Snowflake::new(WORKER_ID));
Source

pub fn next(&mut self) -> &Snowflake

Generates the next snowflake ID and returns a reference to it.

This method generates the next snowflake ID by updating the last_snowflake stored in the generator. It increments the sequence number and, if necessary, adjusts the timestamp to ensure uniqueness. The generated snowflake ID is then returned as a reference.

§Examples
use rusty_snowflake::SnowflakeGenerator;

let mut generator = SnowflakeGenerator::new(420);

// Generate the next snowflake ID
let snowflake = generator.next();
println!("Generated snowflake ID: {:?}", snowflake);
§Returns

A reference to the generated snowflake ID.

§Panics

This method does not panic.

Source

pub fn get_timestamp() -> u64

Get the current timestamp in seconds since the epoch (1970-01-01 00:00:00 UTC).

§Returns

The current timestamp in seconds

Source

pub fn wait_next_timestamp(last_timestamp: u64) -> u64

Wait for the next second and return the timestamp

§Arguments
  • current_timestamp - The current timestamp in seconds
§Returns

The timestamp of the next second

Trait Implementations§

Source§

impl Clone for SnowflakeGenerator

Source§

fn clone(&self) -> SnowflakeGenerator

Returns a copy 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 SnowflakeGenerator

Source§

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

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

impl PartialEq for SnowflakeGenerator

Source§

fn eq(&self, other: &SnowflakeGenerator) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for SnowflakeGenerator

Source§

impl Eq for SnowflakeGenerator

Source§

impl StructuralPartialEq for SnowflakeGenerator

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.