Skip to main content

Snowflake

Trait Snowflake 

Source
pub trait Snowflake {
    // Required method
    fn id(&self) -> u64;

    // Provided methods
    fn timestamp(&self) -> i64 { ... }
    fn worker_id(&self) -> u8 { ... }
    fn process_id(&self) -> u8 { ... }
    fn increment(&self) -> u16 { ... }
}
Available on crate feature snowflake only.
Expand description

Snowflake is a trait for defining extractable information from a Snowflake. A Snowflake is a u64 generated by Discord to uniquely identify a resource.

Required Methods§

Source

fn id(&self) -> u64

Returns the u64 backing the Snowflake.

Provided Methods§

Source

fn timestamp(&self) -> i64

The Unix epoch of the Snowflake in milliseconds, indicating when it was generated.

Derived from bits 22..63 of the id.

§Examples

See when a user was created using chrono:

use chrono::{TimeZone, Utc};
use twilight_model::id::{Id, marker::UserMarker};
use twilight_util::snowflake::Snowflake;

let id = Id::<UserMarker>::new(105484726235607040);

assert_eq!(
    "2015-10-19T01:58:38.546+00:00",
    Utc.timestamp_millis(id.timestamp()).to_rfc3339()
);

See when a user was created using time:

use time::{Duration, OffsetDateTime, format_description::well_known::Rfc3339};
use twilight_model::id::{Id, marker::UserMarker};
use twilight_util::snowflake::Snowflake;

let id = Id::<UserMarker>::new(105484726235607040);
// Convert milliseconds to seconds or nanoseconds.
let dur = Duration::milliseconds(id.timestamp());

let ts = OffsetDateTime::from_unix_timestamp(dur.whole_seconds())?;
let ts_milli = OffsetDateTime::from_unix_timestamp_nanos(dur.whole_nanoseconds())?;

assert_eq!("2015-10-19T01:58:38Z", ts.format(&Rfc3339)?);
assert_eq!("2015-10-19T01:58:38.546Z", ts_milli.format(&Rfc3339)?);
Source

fn worker_id(&self) -> u8

The id of the internal worker that generated the Snowflake.

Derived from bits 17..21 of the id.

Source

fn process_id(&self) -> u8

The id of the internal process that generated the Snowflake.

Derived from bits 12..16 of the id.

Source

fn increment(&self) -> u16

The increment of the Snowflake. For every id that is generated on a process, this number is incremented.

Derived from bits 0..11 of the id.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementations on Foreign Types§

Source§

impl Snowflake for Id<ApplicationMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<AttachmentMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<AuditLogEntryMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<ChannelMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<CommandMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<CommandVersionMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<EmojiMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<GenericMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<GuildMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<IntegrationMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<InteractionMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<MessageMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<OauthSkuMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<OauthTeamMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<RoleMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<RoleSubscriptionSkuMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<ScheduledEventEntityMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<ScheduledEventMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<StageMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<StickerMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<StickerPackMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<StickerPackSkuMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<UserMarker>

Source§

fn id(&self) -> u64

Source§

impl Snowflake for Id<WebhookMarker>

Source§

fn id(&self) -> u64

Implementors§