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
impl SnowflakeGenerator
Sourcepub fn new(worker_id: u64) -> SnowflakeGenerator
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));
Sourcepub fn start_at(snowflake: &Snowflake) -> SnowflakeGenerator
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));
Sourcepub fn next(&mut self) -> &Snowflake
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.
Sourcepub fn get_timestamp() -> u64
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
Trait Implementations§
Source§impl Clone for SnowflakeGenerator
impl Clone for SnowflakeGenerator
Source§fn clone(&self) -> SnowflakeGenerator
fn clone(&self) -> SnowflakeGenerator
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for SnowflakeGenerator
impl Debug for SnowflakeGenerator
Source§impl PartialEq for SnowflakeGenerator
impl PartialEq for SnowflakeGenerator
impl Copy for SnowflakeGenerator
impl Eq for SnowflakeGenerator
impl StructuralPartialEq for SnowflakeGenerator
Auto Trait Implementations§
impl Freeze for SnowflakeGenerator
impl RefUnwindSafe for SnowflakeGenerator
impl Send for SnowflakeGenerator
impl Sync for SnowflakeGenerator
impl Unpin for SnowflakeGenerator
impl UnwindSafe for SnowflakeGenerator
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