Skip to main content

Id

Struct Id 

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

Source

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);
Source

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());
Source

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).

Source

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).

Source

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).

Source

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());
Source

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.

Source

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");
Source

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).

Source

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).

Source

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).

Source

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§

Source§

impl<T> Clone for Id<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Copy for Id<T>

Source§

impl<T> Debug for Id<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, T> Deserialize<'de> for Id<T>

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T> Display for Id<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Eq for Id<T>

Source§

impl<T> From<Id<T>> for NonZeroU64

Source§

fn from(id: Id<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Id<T>> for u64

Source§

fn from(id: Id<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<NonZero<u64>> for Id<T>

Source§

fn from(value: NonZeroU64) -> Self

Converts to this type from the input type.
Source§

impl<T> FromStr for Id<T>

Source§

type Err = ParseIdError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<T> Hash for Id<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> Ord for Id<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl<T> PartialEq for Id<T>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T> PartialOrd for Id<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T> Serialize for Id<T>

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl<T> TryFrom<u64> for Id<T>

Source§

type Error = ParseIdError

The type returned in the event of a conversion error.
Source§

fn try_from(value: u64) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<T> Freeze 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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> ToCompactString for T
where T: Display,

§

fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>

Fallible version of [ToCompactString::to_compact_string()] Read more
§

fn to_compact_string(&self) -> CompactString

Converts the given value to a [CompactString]. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.