pub struct Id<T> { /* private fields */ }Expand description
A typed Discord snowflake ID.
T is a marker type from marker that prevents mixing different kinds of IDs at compile
time. Use the aliases in this module (UserId, GuildId, …) rather than spelling out
Id<UserMarker>.
IDs are ordered chronologically: an ID created later always compares greater.
discord.js counterpart: Snowflake (a string in discord.js).
§Examples
use djs_model::id::GuildId;
let id: GuildId = "175928847299117063".parse().unwrap();
assert_eq!(id.get(), 175_928_847_299_117_063);
assert_eq!(id.to_string(), "175928847299117063");
// Snowflakes embed their creation time.
assert_eq!(id.timestamp_ms(), 1_462_015_105_796);Implementations§
Source§impl<T> Id<T>
impl<T> Id<T>
Sourcepub const fn new(value: u64) -> Self
pub const fn new(value: u64) -> Self
Creates an ID from a raw non-zero value.
discord.js counterpart: none (a Snowflake is a string in discord.js).
§Panics
Panics if value is 0. Use Id::new_checked for fallible construction.
§Examples
use djs_model::id::UserId;
const BOT_OWNER: UserId = UserId::new(80_351_110_224_678_912);
assert_eq!(BOT_OWNER.get(), 80_351_110_224_678_912);Sourcepub const fn new_checked(value: u64) -> Option<Self>
pub const fn new_checked(value: u64) -> Option<Self>
Creates an ID from a raw value, returning None if it is 0.
discord.js counterpart: none (a Snowflake is a string in discord.js).
§Examples
use djs_model::id::UserId;
assert!(UserId::new_checked(0).is_none());
assert!(UserId::new_checked(1).is_some());Sourcepub const fn from_nonzero(value: NonZeroU64) -> Self
pub const fn from_nonzero(value: NonZeroU64) -> Self
Creates an ID from a NonZeroU64. This can never fail.
discord.js counterpart: none (a Snowflake is a string in discord.js).
Sourcepub const fn get(self) -> u64
pub const fn get(self) -> u64
Returns the raw value of the ID.
discord.js counterpart: none (a Snowflake is a string in discord.js).
Sourcepub const fn into_nonzero(self) -> NonZeroU64
pub const fn into_nonzero(self) -> NonZeroU64
Returns the raw value of the ID as a NonZeroU64.
discord.js counterpart: none (a Snowflake is a string in discord.js).
Sourcepub const fn cast<U>(self) -> Id<U>
pub const fn cast<U>(self) -> Id<U>
Reinterprets this ID as an ID of another kind.
Some Discord resources deliberately share IDs — for example the @everyone role of a
guild has the same ID as the guild itself. cast makes such conversions explicit.
discord.js counterpart: none (a Snowflake is a string in discord.js).
§Examples
use djs_model::id::{GuildId, RoleId};
let guild = GuildId::new(175_928_847_299_117_063);
let everyone: RoleId = guild.cast();
assert_eq!(everyone.get(), guild.get());Sourcepub const fn timestamp_ms(self) -> u64
pub const fn timestamp_ms(self) -> u64
Returns the creation time encoded in the snowflake, in milliseconds since the Unix epoch.
discord.js counterpart: SnowflakeUtil.timestampFrom.
Sourcepub const fn timestamp(self) -> Timestamp
pub const fn timestamp(self) -> Timestamp
Returns the creation time encoded in the snowflake.
discord.js counterpart: the createdAt / createdTimestamp getters on structures.
§Examples
use djs_model::id::UserId;
let id = UserId::new(80_351_110_224_678_912);
assert_eq!(id.timestamp().to_string(), "2015-08-10T17:26:37.529000+00:00");Sourcepub const fn worker_id(self) -> u8
pub const fn worker_id(self) -> u8
Returns the internal worker ID encoded in the snowflake (5 bits).
discord.js counterpart: SnowflakeUtil.deconstruct’s workerId (@sapphire/snowflake).
Sourcepub const fn process_id(self) -> u8
pub const fn process_id(self) -> u8
Returns the internal process ID encoded in the snowflake (5 bits).
discord.js counterpart: SnowflakeUtil.deconstruct’s processId (@sapphire/snowflake).
Sourcepub const fn increment(self) -> u16
pub const fn increment(self) -> u16
Returns the per-process increment encoded in the snowflake (12 bits).
discord.js counterpart: SnowflakeUtil.deconstruct’s increment (@sapphire/snowflake).
Sourcepub const fn from_timestamp_ms(unix_ms: u64) -> Option<Self>
pub const fn from_timestamp_ms(unix_ms: u64) -> Option<Self>
Creates the smallest ID that could have been generated at unix_ms
(milliseconds since the Unix epoch).
This is useful for paginating REST endpoints by time (before / after parameters).
Returns None if unix_ms is at or before the Discord epoch (the ID would be 0) or so
far in the future that the timestamp does not fit in a snowflake.
discord.js counterpart: SnowflakeUtil.generate({ timestamp }).
§Examples
use djs_model::id::MessageId;
let id = MessageId::from_timestamp_ms(1_462_015_105_796).unwrap();
assert_eq!(id.timestamp_ms(), 1_462_015_105_796);Trait Implementations§
impl<T> Copy for Id<T>
Source§impl<'de, T> Deserialize<'de> for Id<T>
impl<'de, T> Deserialize<'de> for Id<T>
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
impl<T> Eq for Id<T>
Source§impl<T> From<Id<T>> for NonZeroU64
impl<T> From<Id<T>> for NonZeroU64
Source§impl<T> From<NonZero<u64>> for Id<T>
impl<T> From<NonZero<u64>> for Id<T>
Source§fn from(value: NonZeroU64) -> Self
fn from(value: NonZeroU64) -> Self
Source§impl<T> Ord for Id<T>
impl<T> Ord for Id<T>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T> PartialOrd for Id<T>
impl<T> PartialOrd for Id<T>
Auto Trait Implementations§
impl<T> Forget for Id<T>
impl<T> Freeze for Id<T>
impl<T> Move for Id<T>
impl<T> RefUnwindSafe for Id<T>
impl<T> Send for Id<T>
impl<T> Sync for Id<T>
impl<T> Unpin for Id<T>
impl<T> UnsafeUnpin for Id<T>
impl<T> UnwindSafe for Id<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string() Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read more