Trait Snowflake

Source
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§

Source

const ZERO: Self::Ty

Zero value (used for resetting the sequence)

Source

const ONE: Self::Ty

One value (used for incrementing the sequence)

Required Associated Types§

Source

type Ty: Ord + Copy + Add<Output = Self::Ty> + Into<Self::Ty> + From<Self::Ty>

Scalar type for all bit fields (typically u64)

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 to_raw(&self) -> Self::Ty

Converts this type into its raw type representation

Source

fn from_raw(raw: Self::Ty) -> Self

Converts a raw type into this type

Source

fn to_padded_string(&self) -> String

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", so this trait is not object safe.

Implementors§

Source§

impl Snowflake for SnowflakeDiscordId

Source§

const ZERO: Self::Ty = {transmute(0x0000000000000000): <id::SnowflakeDiscordId as id::Snowflake>::Ty}

Source§

const ONE: Self::Ty = {transmute(0x0000000000000001): <id::SnowflakeDiscordId as id::Snowflake>::Ty}

Source§

type Ty = u64

Source§

impl Snowflake for SnowflakeInstagramId

Source§

const ZERO: Self::Ty = {transmute(0x0000000000000000): <id::SnowflakeInstagramId as id::Snowflake>::Ty}

Source§

const ONE: Self::Ty = {transmute(0x0000000000000001): <id::SnowflakeInstagramId as id::Snowflake>::Ty}

Source§

type Ty = u64

Source§

impl Snowflake for SnowflakeMastodonId

Source§

const ZERO: Self::Ty = {transmute(0x0000000000000000): <id::SnowflakeMastodonId as id::Snowflake>::Ty}

Source§

const ONE: Self::Ty = {transmute(0x0000000000000001): <id::SnowflakeMastodonId as id::Snowflake>::Ty}

Source§

type Ty = u64

Source§

impl Snowflake for SnowflakeTwitterId

Source§

const ZERO: Self::Ty = {transmute(0x0000000000000000): <id::SnowflakeTwitterId as id::Snowflake>::Ty}

Source§

const ONE: Self::Ty = {transmute(0x0000000000000001): <id::SnowflakeTwitterId as id::Snowflake>::Ty}

Source§

type Ty = u64