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.

Implementations

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

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.

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]);

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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.

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

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.

The associated error which can be returned from parsing.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

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.

The type returned in the event of a conversion 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.

The type returned in the event of a conversion error.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more