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:

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

Enums§