use std::io::{self, Read};
use crate::{
fmt::HexDebug,
serialization::{ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize},
};
#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(
any(test, feature = "proptest-impl"),
derive(proptest_derive::Arbitrary)
)]
pub struct Mac(HexDebug<[u8; 32]>);
impl From<[u8; 32]> for Mac {
fn from(bytes: [u8; 32]) -> Self {
Self(bytes.into())
}
}
impl From<Mac> for [u8; 32] {
fn from(rt: Mac) -> [u8; 32] {
*rt.0
}
}
impl From<&Mac> for [u8; 32] {
fn from(mac: &Mac) -> Self {
mac.clone().into()
}
}
impl ZcashDeserialize for Mac {
fn zcash_deserialize<R: Read>(mut reader: R) -> Result<Self, SerializationError> {
let bytes = reader.read_32_bytes()?;
Ok(Self(bytes.into()))
}
}
impl ZcashSerialize for Mac {
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
writer.write_all(&self.0[..])
}
}