Skip to main content

SnowflakeId

Trait SnowflakeId 

Source
pub trait SnowflakeId: Id {
Show 13 methods // Required methods fn timestamp(&self) -> Self::Ty; fn max_timestamp() -> Self::Ty; fn machine_id(&self) -> Self::Ty; fn max_machine_id() -> Self::Ty; fn sequence(&self) -> Self::Ty; fn max_sequence() -> Self::Ty; fn from_components( timestamp: Self::Ty, machine_id: Self::Ty, sequence: Self::Ty, ) -> Self; fn is_valid(&self) -> bool; fn into_valid(self) -> Self; // Provided methods fn has_sequence_room(&self) -> bool { ... } fn next_sequence(&self) -> Self::Ty { ... } fn increment_sequence(&self) -> Self { ... } fn rollover_to_timestamp(&self, ts: Self::Ty) -> Self { ... }
}
Available on crate feature snowflake only.
Expand description

A trait representing a layout-compatible Snowflake ID generator.

This trait abstracts the core behavior of a Snowflake-style ID with separate bit fields for timestamp, machine ID, and sequence.

Types implementing this trait can define custom bit layouts and time units.

§Example

use ferroid::id::{SnowflakeId, SnowflakeTwitterId};

let id = SnowflakeTwitterId::from_components(1000, 2, 1);
assert_eq!(id.timestamp(), 1000);
assert_eq!(id.machine_id(), 2);
assert_eq!(id.sequence(), 1);

Required Methods§

Source

fn timestamp(&self) -> Self::Ty

Returns the timestamp portion of the ID.

Source

fn max_timestamp() -> Self::Ty

Returns the maximum possible value for the timestamp field.

Source

fn machine_id(&self) -> Self::Ty

Returns the machine ID portion of the ID.

Source

fn max_machine_id() -> Self::Ty

Returns the maximum possible value for the machine_id field.

Source

fn sequence(&self) -> Self::Ty

Returns the sequence portion of the ID.

Source

fn max_sequence() -> Self::Ty

Returns the maximum possible value for the sequence field.

Source

fn from_components( timestamp: Self::Ty, machine_id: Self::Ty, sequence: Self::Ty, ) -> Self

Constructs a new Snowflake ID from its components.

Source

fn is_valid(&self) -> bool

Returns true if the ID’s internal structure is valid, such as reserved bits being unset or fields within expected ranges.

Source

fn into_valid(self) -> Self

Returns a normalized version of the ID with any invalid or reserved bits cleared. This guarantees a valid, canonical representation.

Provided Methods§

Source

fn has_sequence_room(&self) -> bool

Returns true if the current sequence value can be incremented.

Source

fn next_sequence(&self) -> Self::Ty

Returns the next sequence value.

Source

fn increment_sequence(&self) -> Self

Returns a new ID with the sequence incremented.

Source

fn rollover_to_timestamp(&self, ts: Self::Ty) -> Self

Returns a new ID for a newer timestamp with sequence reset to zero.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§