use std::str::FromStr;
use hex::FromHex;
use hex::ToHex;
use serde_with::DeserializeFromStr;
use serde_with::SerializeDisplay;
pub const RANDOMX_RESULT_SIZE: usize = 32;
pub type ResultHashInner = [u8; RANDOMX_RESULT_SIZE];
#[derive(
Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, SerializeDisplay, DeserializeFromStr,
)]
#[repr(transparent)]
pub struct ResultHash(ResultHashInner);
impl ResultHash {
pub fn from_slice(hash: ResultHashInner) -> Self {
Self(hash)
}
pub fn into_slice(self) -> ResultHashInner {
self.0
}
}
impl AsRef<ResultHashInner> for ResultHash {
fn as_ref(&self) -> &ResultHashInner {
&self.0
}
}
impl AsMut<ResultHashInner> for ResultHash {
fn as_mut(&mut self) -> &mut ResultHashInner {
&mut self.0
}
}
impl FromHex for ResultHash {
type Error = <[u8; 32] as FromHex>::Error;
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
ResultHashInner::from_hex(hex).map(Self)
}
}
impl ToHex for ResultHash {
fn encode_hex<T: std::iter::FromIterator<char>>(&self) -> T {
ToHex::encode_hex(&self.0)
}
fn encode_hex_upper<T: std::iter::FromIterator<char>>(&self) -> T {
ToHex::encode_hex_upper(&self.0)
}
}
impl FromStr for ResultHash {
type Err = hex::FromHexError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
FromHex::from_hex(s)
}
}
use std::fmt;
impl fmt::Display for ResultHash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.encode_hex::<String>())
}
}
impl fmt::Debug for ResultHash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("ResultHash")
.field(&self.to_string())
.finish()
}
}