Struct twilight_model::id::Id

source ·
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>

source

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.

source

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.

source

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.

source

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

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

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§

source§

impl<T> Clone for Id<T>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<T> Debug for Id<T>

source§

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

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<'_>) -> FmtResult

Formats the value using the given formatter. Read more
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(id: NonZeroU64) -> Self

Converts to this type from the input type.
source§

impl<T> FromStr for Id<T>

§

type Err = ParseIntError

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<U: Hasher>(&self, state: &mut U)

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

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

Compares and returns the maximum of two values. Read more
1.21.0 · source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · source§

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

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

impl<T> PartialEq<Id<T>> for i64

source§

fn eq(&self, other: &Id<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> PartialEq<Id<T>> for u64

source§

fn eq(&self, other: &Id<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> PartialEq<i64> for Id<T>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> PartialEq<u64> for Id<T>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> PartialEq for Id<T>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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 · source§

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

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
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method 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<i64> for Id<T>

§

type Error = TryFromIntError

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

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

Performs the conversion.
source§

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

§

type Error = TryFromIntError

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

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

Performs the conversion.
source§

impl<T> Copy for Id<T>

source§

impl<T> Eq for Id<T>

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

source§

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

§

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§

default 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>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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

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