twilight_model::id

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

Source§

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,

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

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

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

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

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>

Source§

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>

Source§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

Source§

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

Source§

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

Source§

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