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 { ... }
}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::{SnowflakeId, SnowflakeTwitterId};
let id = SnowflakeTwitterId::from(1000, 2, 1);
assert_eq!(id.timestamp(), 1000);
assert_eq!(id.machine_id(), 2);
assert_eq!(id.sequence(), 1);Required Methods§
Sourcefn max_timestamp() -> Self::Ty
fn max_timestamp() -> Self::Ty
Returns the maximum possible value for the timestamp field.
Sourcefn machine_id(&self) -> Self::Ty
fn machine_id(&self) -> Self::Ty
Returns the machine ID portion of the ID.
Sourcefn max_machine_id() -> Self::Ty
fn max_machine_id() -> Self::Ty
Returns the maximum possible value for the machine_id field.
Sourcefn max_sequence() -> Self::Ty
fn max_sequence() -> Self::Ty
Returns the maximum possible value for the sequence field.
Sourcefn from_components(
timestamp: Self::Ty,
machine_id: Self::Ty,
sequence: Self::Ty,
) -> Self
fn from_components( timestamp: Self::Ty, machine_id: Self::Ty, sequence: Self::Ty, ) -> Self
Constructs a new Snowflake ID from its components.
Sourcefn is_valid(&self) -> bool
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.
Sourcefn into_valid(self) -> Self
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§
Sourcefn has_sequence_room(&self) -> bool
fn has_sequence_room(&self) -> bool
Returns true if the current sequence value can be incremented.
Sourcefn next_sequence(&self) -> Self::Ty
fn next_sequence(&self) -> Self::Ty
Returns the next sequence value.
Sourcefn increment_sequence(&self) -> Self
fn increment_sequence(&self) -> Self
Returns a new ID with the sequence incremented.
Sourcefn rollover_to_timestamp(&self, ts: Self::Ty) -> Self
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", so this trait is not object safe.