pub trait Snowflake:
Sized
+ Copy
+ Display {
type Ty: Ord + Copy + Add<Output = Self::Ty> + Into<Self::Ty> + From<Self::Ty>;
const ZERO: Self::Ty;
const ONE: Self::Ty;
Show 14 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 to_raw(&self) -> Self::Ty;
fn from_raw(raw: Self::Ty) -> Self;
fn to_padded_string(&self) -> String;
// 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 { ... }
}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::{Snowflake, 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 Associated Constants§
Required Associated Types§
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.
fn to_padded_string(&self) -> String
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.