Struct twilight_model::util::image_hash::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.
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
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.
type Err = ImageHashParseError
type Err = ImageHashParseError
The associated error which can be returned from parsing.
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.
type Error = ImageHashParseError
type Error = ImageHashParseError
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.
type Error = ImageHashParseError
type Error = ImageHashParseError
The type returned in the event of a conversion error.
Auto Trait Implementations
impl RefUnwindSafe for ImageHash
impl UnwindSafe for ImageHash
Blanket Implementations
Mutably borrows from an owned value. Read more
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