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::String
s.
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
impl ImageHash
sourcepub const fn new(bytes: [u8; 16], animated: bool) -> Self
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());
sourcepub const fn parse(value: &[u8]) -> Result<Self, ImageHashParseError>
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.
sourcepub const fn bytes(self) -> [u8; 16]
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]);
sourcepub const fn is_animated(self) -> bool
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());
sourcepub const fn nibbles(self) -> Nibbles ⓘ
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<'de> Deserialize<'de> for ImageHash
impl<'de> Deserialize<'de> for ImageHash
source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
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
impl Display for ImageHash
source§fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult
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
impl FromStr for ImageHash
source§fn from_str(s: &str) -> Result<Self, Self::Err>
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
type Err = ImageHashParseError
source§impl TryFrom<&[u8]> for ImageHash
impl TryFrom<&[u8]> for ImageHash
source§fn try_from(value: &[u8]) -> Result<Self, Self::Error>
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
type Error = ImageHashParseError
source§impl TryFrom<&str> for ImageHash
impl TryFrom<&str> for ImageHash
source§fn try_from(value: &str) -> Result<Self, Self::Error>
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
type Error = ImageHashParseError
impl Copy for ImageHash
impl Eq for ImageHash
impl StructuralPartialEq for ImageHash
Auto Trait Implementations§
impl Freeze for ImageHash
impl RefUnwindSafe for ImageHash
impl Send for ImageHash
impl Sync for ImageHash
impl Unpin for ImageHash
impl UnwindSafe for ImageHash
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)