Trait twilight_util::snowflake::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::{marker::UserMarker, Id};
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::{format_description::well_known::Rfc3339, Duration, OffsetDateTime};
use twilight_model::id::{marker::UserMarker, Id};
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.

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§