Module twilight_model::id
source · [−]Expand description
ID with type-safe markers for each resource.
When IDs are simple 64-bit integers then it may be easy to accidentally use the ID of a role in place of where one means to use the ID of a user. This is a programmatic error; it’s on the programmer to notice. By using IDs with typed markers, it can be ensured that only an ID with a guild marker is used where an ID with a guild marker is requested.
Parsing
IDs may be initialized or parsed in a variety of manners depending on the context:
serde
deserializationstd::str::FromStr
std::convert::TryFrom
<i64>std::convert::TryFrom
<u64>Id::new
Id::new_checked
Id::new_unchecked
std::convert::From
<std::num::NonZeroU64
>
Casting between resource types
Discord may have constraints where IDs are the same across resources. For
example, the @everyone
role of a guild has the same ID as the guild
itself. In this case, all one needs to do is use the guild’s ID in place of
a role in order to operate on the @everyone
role of the guild. IDs can be
easily casted in order to fulfill this:
use twilight_model::id::{marker::{GuildMarker, RoleMarker}, Id};
// Often Rust's type inference will be able to infer the type of ID.
let guild_id = Id::<GuildMarker>::new(123);
let role_id = guild_id.cast::<RoleMarker>();
assert_eq!(guild_id.get(), role_id.get());
Modules
Markers for various resource types, such as channels or users.
Structs
Type Definitions
ID with an application marker.
ID with an attachment marker.
ID with an audit log entry marker.
ID with a channel marker.
ID with a command marker.
ID with a command marker.
ID with an emoji marker.
ID with a generic marker.
ID with a guild marker.
ID with an integration marker.
ID with an interaction marker.
ID with a message marker.
ID with an OAuth SKU marker.
ID with an OAuth team marker.
ID with a role marker.
ID with a stage marker.
ID with a sticker banner asset marker.
ID with a sticker marker.
ID with a sticker pack marker.
ID with a sticker pack SKU marker.
ID with a user marker.
ID with a webhook marker.