pub struct Id<T> { /* private fields */ }
Expand description
ID of a resource, such as the ID of a channel or user.
Markers themselves perform no logical action, and are only used to ensure that IDs of incorrect types aren’t used. Read the marker documentation for additional information.
§serde
This ID deserializes from both integers and strings and serializes into a string.
Implementations§
Source§impl<T> Id<T>
impl<T> Id<T>
Sourcepub const fn new(n: u64) -> Self
pub const fn new(n: u64) -> Self
Create a new ID, panicking if the value is zero.
This is primarily useful in const contexts where you are passing a hardcoded value.
Refer to new_checked
for a checked alternative to this method.
§Examples
use twilight_model::id::{marker::GenericMarker, Id};
const ID: Id<GenericMarker> = Id::new(123);
println!("id: {ID}");
§Panics
Panics if the value is 0.
Sourcepub const unsafe fn new_unchecked(n: u64) -> Self
pub const unsafe fn new_unchecked(n: u64) -> Self
Create an ID without checking if the value is non-zero.
Equivalent to NonZeroU64::new_unchecked
.
§Safety
The value must not be zero.
Sourcepub const fn new_checked(n: u64) -> Option<Self>
pub const fn new_checked(n: u64) -> Option<Self>
Create an ID if the provided value is not zero.
§Examples
use twilight_model::id::{marker::GenericMarker, Id};
assert!(Id::<GenericMarker>::new_checked(123).is_some());
assert!(Id::<GenericMarker>::new_checked(0).is_none());
Equivalent to NonZeroU64::new
.
Sourcepub const fn get(self) -> u64
pub const fn get(self) -> u64
Return the inner primitive value.
Equivalent to NonZeroU64::get
.
§Examples
Create an ID with a value and then confirm its inner value:
use twilight_model::id::{marker::ChannelMarker, Id};
let channel_id = Id::<ChannelMarker>::new(7);
assert_eq!(7, channel_id.get());
Sourcepub const fn into_nonzero(self) -> NonZeroU64
pub const fn into_nonzero(self) -> NonZeroU64
Return the NonZeroU64
representation of the ID.
§Examples
Create an ID with a value and then confirm its nonzero value:
use std::num::NonZeroU64;
use twilight_model::id::{marker::ChannelMarker, Id};
let channel_id = Id::<ChannelMarker>::new(7);
assert_eq!(NonZeroU64::new(7).unwrap(), channel_id.into_nonzero());
Sourcepub const fn cast<New>(self) -> Id<New>
pub const fn cast<New>(self) -> Id<New>
Cast an ID from one type to another.
§Examples
Cast a role ID to a guild ID, useful for the @everyone
role:
use twilight_model::id::{
marker::{GuildMarker, RoleMarker},
Id,
};
let role_id: Id<RoleMarker> = Id::new(1);
let guild_id: Id<GuildMarker> = role_id.cast();
assert_eq!(1, guild_id.get());