twilight_util/
snowflake.rsuse twilight_model::id::{
marker::{
ApplicationMarker, AttachmentMarker, AuditLogEntryMarker, ChannelMarker, CommandMarker,
CommandVersionMarker, EmojiMarker, GenericMarker, GuildMarker, IntegrationMarker,
InteractionMarker, MessageMarker, OauthSkuMarker, OauthTeamMarker, RoleMarker,
RoleSubscriptionSkuMarker, ScheduledEventEntityMarker, ScheduledEventMarker, StageMarker,
StickerMarker, StickerPackMarker, StickerPackSkuMarker, UserMarker, WebhookMarker,
},
Id,
};
pub trait Snowflake {
fn id(&self) -> u64;
#[allow(clippy::cast_possible_wrap)]
fn timestamp(&self) -> i64 {
const DISCORD_EPOCH: u64 = 1_420_070_400_000;
((self.id() >> 22) + DISCORD_EPOCH) as i64
}
#[allow(clippy::cast_possible_truncation)]
fn worker_id(&self) -> u8 {
((self.id() & 0x003E_0000) >> 17) as u8
}
#[allow(clippy::cast_possible_truncation)]
fn process_id(&self) -> u8 {
((self.id() & 0x1F000) >> 12) as u8
}
#[allow(clippy::cast_possible_truncation)]
fn increment(&self) -> u16 {
(self.id() & 0xFFF) as u16
}
}
impl Snowflake for Id<ApplicationMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<AttachmentMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<AuditLogEntryMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<ChannelMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<CommandMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<CommandVersionMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<EmojiMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<GenericMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<GuildMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<IntegrationMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<InteractionMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<MessageMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<OauthSkuMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<OauthTeamMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<RoleMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<RoleSubscriptionSkuMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<ScheduledEventMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<ScheduledEventEntityMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<StageMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<StickerMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<StickerPackMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<StickerPackSkuMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<UserMarker> {
fn id(&self) -> u64 {
self.get()
}
}
impl Snowflake for Id<WebhookMarker> {
fn id(&self) -> u64 {
self.get()
}
}
#[cfg(test)]
mod tests {
use super::*;
use static_assertions::{assert_impl_all, assert_obj_safe};
assert_impl_all!(Id<ApplicationMarker>: Snowflake);
assert_impl_all!(Id<AttachmentMarker>: Snowflake);
assert_impl_all!(Id<AuditLogEntryMarker>: Snowflake);
assert_impl_all!(Id<ChannelMarker>: Snowflake);
assert_impl_all!(Id<CommandMarker>: Snowflake);
assert_impl_all!(Id<CommandVersionMarker>: Snowflake);
assert_impl_all!(Id<EmojiMarker>: Snowflake);
assert_impl_all!(Id<GenericMarker>: Snowflake);
assert_impl_all!(Id<GuildMarker>: Snowflake);
assert_impl_all!(Id<IntegrationMarker>: Snowflake);
assert_impl_all!(Id<InteractionMarker>: Snowflake);
assert_impl_all!(Id<MessageMarker>: Snowflake);
assert_impl_all!(Id<OauthSkuMarker>: Snowflake);
assert_impl_all!(Id<OauthTeamMarker>: Snowflake);
assert_impl_all!(Id<RoleMarker>: Snowflake);
assert_impl_all!(Id<RoleSubscriptionSkuMarker>: Snowflake);
assert_impl_all!(Id<ScheduledEventMarker>: Snowflake);
assert_impl_all!(Id<ScheduledEventEntityMarker>: Snowflake);
assert_impl_all!(Id<StageMarker>: Snowflake);
assert_impl_all!(Id<StickerMarker>: Snowflake);
assert_impl_all!(Id<StickerPackMarker>: Snowflake);
assert_impl_all!(Id<StickerPackSkuMarker>: Snowflake);
assert_impl_all!(Id<UserMarker>: Snowflake);
assert_impl_all!(Id<WebhookMarker>: Snowflake);
assert_obj_safe!(Snowflake);
#[test]
fn timestamp() {
let expected: i64 = 1_445_219_918_546;
let id = Id::<GenericMarker>::new(105_484_726_235_607_040);
assert_eq!(expected, id.timestamp());
}
#[test]
fn worker_id() {
let expected: u8 = 8;
let id = Id::<GenericMarker>::new(762_022_344_856_174_632);
assert_eq!(expected, id.worker_id());
}
#[test]
fn process_id() {
let expected: u8 = 1;
let id = Id::<GenericMarker>::new(61_189_081_970_774_016);
assert_eq!(expected, id.process_id());
}
#[test]
fn increment() {
let expected: u16 = 40;
let id = Id::<GenericMarker>::new(762_022_344_856_174_632);
assert_eq!(expected, id.increment());
}
}