pub struct Snowflake { /* private fields */ }
Expand description
Snowflake ID generator
This struct implements the Snowflake algorithm for generating unique IDs. Each ID is composed of:
- Timestamp (41 bits)
- Node ID (10 bits)
- Sequence number (12 bits)
Implementations§
Source§impl Snowflake
impl Snowflake
Sourcepub fn new(node: u16, epoch: Option<i64>) -> Result<Self, SnowflakeError>
pub fn new(node: u16, epoch: Option<i64>) -> Result<Self, SnowflakeError>
Creates a new Snowflake instance
§Arguments
node
- A unique identifier for the node generating the IDs (0-1023)epoch
- An optional custom epoch in milliseconds. If None, DEFAULT_EPOCH is used.
§Returns
A Result containing the new Snowflake instance or a SnowflakeError
§Errors
Returns SnowflakeError::MachineIdOutOfRange if the node ID is greater than 1023
Sourcepub fn generate(&self) -> Result<u64, SnowflakeError>
pub fn generate(&self) -> Result<u64, SnowflakeError>
Sourcepub fn parse_id(id: u64) -> (u64, u16, u16)
pub fn parse_id(id: u64) -> (u64, u16, u16)
Parses a Snowflake ID into its components
§Arguments
id
- The Snowflake ID to parse
§Returns
A tuple containing the timestamp, node ID, and sequence number
§Example
let (timestamp, node, sequence) = Snowflake::parse_id(1234567890);
println!("Timestamp: {}, Node: {}, Sequence: {}", timestamp, node, sequence);
Auto Trait Implementations§
impl !Freeze for Snowflake
impl RefUnwindSafe for Snowflake
impl Send for Snowflake
impl Sync for Snowflake
impl Unpin for Snowflake
impl UnwindSafe for Snowflake
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