Struct twilight_model::id::Id
source · [−]#[repr(transparent)]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
sourceimpl<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 a non-zero application ID without checking the value.
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 a non-zero application ID, checking if the provided value is 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());
Trait Implementations
sourceimpl<'de, T> Deserialize<'de> for Id<T>
impl<'de, T> Deserialize<'de> for Id<T>
sourcefn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Deserialize this value from the given Serde deserializer. Read more
sourceimpl<T> From<Id<T>> for NonZeroU64
impl<T> From<Id<T>> for NonZeroU64
sourceimpl<T> From<NonZeroU64> for Id<T>
impl<T> From<NonZeroU64> for Id<T>
sourcefn from(id: NonZeroU64) -> Self
fn from(id: NonZeroU64) -> Self
Converts to this type from the input type.
sourceimpl<T> Ord for Id<T>
impl<T> Ord for Id<T>
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
Restrict a value to a certain interval. Read more
sourceimpl<T> PartialOrd<Id<T>> for Id<T>
impl<T> PartialOrd<Id<T>> for Id<T>
sourcefn partial_cmp(&self, other: &Self) -> Option<Ordering>
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 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl<T> Copy for Id<T>
impl<T> Eq for Id<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for Id<T> where
T: RefUnwindSafe,
impl<T> Send for Id<T> where
T: Send,
impl<T> Sync for Id<T> where
T: Sync,
impl<T> Unpin for Id<T> where
T: Unpin,
impl<T> UnwindSafe for Id<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more