twilight_model::util::image_hash

Struct ImageHash

source
pub struct ImageHash { /* private fields */ }
Expand description

Efficient storage for Discord image hashes.

This works by storing image hashes as packed integers rather than heap-allocated std::string::Strings.

Parsing methods only support hashes provided by Discord’s APIs.

Clyde AI has a unique hash that doesn’t match the patterns of other hashes, uniquely processed as ImageHash::CLYDE.

Implementations§

source§

impl ImageHash

source

pub const CLYDE: Self = _

Avatar hash of Clyde AI, which has a non-standard hash.

source

pub const fn new(bytes: [u8; 16], animated: bool) -> Self

Instantiate a new hash from its raw parts.

Parts can be obtained via is_animated and bytes.

§Examples

Parse an image hash, deconstruct it, and then reconstruct it:

use twilight_model::util::ImageHash;

let input = "1acefe340fafb4ecefae407f3abdb323";
let parsed = ImageHash::parse(input.as_bytes())?;

let (bytes, is_animated) = (parsed.bytes(), parsed.is_animated());

let constructed = ImageHash::new(bytes, is_animated);
assert_eq!(input, constructed.to_string());
source

pub const fn parse(value: &[u8]) -> Result<Self, ImageHashParseError>

Parse an image hash into an efficient integer-based storage.

§Examples

Parse a static image hash:

use twilight_model::util::ImageHash;

let input = "b2a6536641da91a0b59bd66557c56c36";
let parsed = ImageHash::parse(input.as_bytes())?;

assert!(!parsed.is_animated());
assert_eq!(input, parsed.to_string());

Parse an animated image hash:

use twilight_model::util::ImageHash;

let input = "a_b2a6536641da91a0b59bd66557c56c36";
let parsed = ImageHash::parse(input.as_bytes())?;

assert!(parsed.is_animated());
assert_eq!(input, parsed.to_string());
§Errors

Returns an ImageHashParseErrorType::Format error type if the provided value isn’t in a Discord image hash format. Refer to the variant’s documentation for more details.

Returns an ImageHashParseErrorType::Range error type if one of the hex values is outside of the accepted range. Refer to the variant’s documentation for more details.

source

pub const fn bytes(self) -> [u8; 16]

Efficient packed bytes of the hash.

Can be paired with is_animated for use in new to recreate the efficient image hash.

§Examples

Parse an image hash and then check the packed bytes:

use twilight_model::util::ImageHash;

let input = b"f49d812ca33c1cbbeec96b9f64487c7c";
let hash = ImageHash::parse(input)?;
let bytes = hash.bytes();

// Byte correlates to 12 (c) followed by 7 (7).
assert_eq!(0b0111_1100, bytes[0]);

// Byte correlates to 4 (4) followed by 15 (f).
assert_eq!(0b1111_0100, bytes[15]);
source

pub const fn is_animated(self) -> bool

Whether the hash is for an animated image.

§Examples

Parse an animated image hash prefixed with a_ and another static image hash that is not prefixed with a_:

use twilight_model::util::ImageHash;

let animated_input = "a_5145104ad8e8c9e765883813e4abbcc8";
let animated_hash = ImageHash::parse(animated_input.as_bytes())?;
assert!(animated_hash.is_animated());

let static_input = "c7e7c4b8469d790cb9b293759e60953d";
let static_hash = ImageHash::parse(static_input.as_bytes())?;
assert!(!static_hash.is_animated());
source

pub const fn nibbles(self) -> Nibbles

Create an iterator over the nibbles of the hexadecimal image hash.

§Examples

Parse an image hash and then iterate over the nibbles:

use twilight_model::util::ImageHash;

let input = b"1d9811c4cd3782148915c522b02878fc";
let hash = ImageHash::parse(input)?;
let mut nibbles = hash.nibbles();

assert_eq!(Some(b'1'), nibbles.next());
assert_eq!(Some(b'd'), nibbles.next());
assert_eq!(Some(b'c'), nibbles.nth(29));
assert!(nibbles.next().is_none());

Trait Implementations§

source§

impl Clone for ImageHash

source§

fn clone(&self) -> ImageHash

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 Debug for ImageHash

source§

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

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for ImageHash

source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Parse an image hash string into an efficient decimal store.

§Examples

Refer to ImageHash::parse’s documentation for examples.

§Errors

Returns an ImageHashParseErrorType::Format error type if the provided value isn’t in a Discord image hash format. Refer to the variant’s documentation for more details.

Returns an ImageHashParseErrorType::Range error type if one of the hex values is outside of the accepted range. Refer to the variant’s documentation for more details.

source§

impl Display for ImageHash

source§

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

Format the image hash as a hex string.

§Examples

Parse a hash and then format it to ensure it matches the input:

use twilight_model::util::ImageHash;

let input = "a_b0e09d6697b11e9c79a89e5e3756ddee";
let parsed = ImageHash::parse(input.as_bytes())?;

assert_eq!(input, parsed.to_string());
source§

impl FromStr for ImageHash

source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parse an image hash string into an efficient decimal store.

§Examples

Refer to ImageHash::parse’s documentation for examples.

§Errors

Returns an ImageHashParseErrorType::Format error type if the provided value isn’t in a Discord image hash format. Refer to the variant’s documentation for more details.

Returns an ImageHashParseErrorType::Range error type if one of the hex values is outside of the accepted range. Refer to the variant’s documentation for more details.

source§

type Err = ImageHashParseError

The associated error which can be returned from parsing.
source§

impl Hash for ImageHash

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

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 PartialEq for ImageHash

source§

fn eq(&self, other: &ImageHash) -> 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 Serialize for ImageHash

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 TryFrom<&[u8]> for ImageHash

source§

fn try_from(value: &[u8]) -> Result<Self, Self::Error>

Parse an image hash string into an efficient decimal store.

§Examples

Refer to ImageHash::parse’s documentation for examples.

§Errors

Returns an ImageHashParseErrorType::Format error type if the provided value isn’t in a Discord image hash format. Refer to the variant’s documentation for more details.

Returns an ImageHashParseErrorType::Range error type if one of the hex values is outside of the accepted range. Refer to the variant’s documentation for more details.

source§

type Error = ImageHashParseError

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

impl TryFrom<&str> for ImageHash

source§

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

Parse an image hash string into an efficient decimal store.

§Examples

Refer to ImageHash::parse’s documentation for examples.

§Errors

Returns an ImageHashParseErrorType::Format error type if the provided value isn’t in a Discord image hash format. Refer to the variant’s documentation for more details.

Returns an ImageHashParseErrorType::Range error type if one of the hex values is outside of the accepted range. Refer to the variant’s documentation for more details.

source§

type Error = ImageHashParseError

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

impl Copy for ImageHash

source§

impl Eq for ImageHash

source§

impl StructuralPartialEq for ImageHash

Auto Trait Implementations§

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 T)

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