use alloc::string::ToString;
use core::fmt;
use corez::io::{self, Read, Write};
use zcash_encoding::ReverseHex;
#[cfg(feature = "std")]
use memuse::DynamicUsage;
#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct TxId([u8; 32]);
#[cfg(feature = "std")]
memuse::impl_no_dynamic_usage!(TxId);
impl fmt::Debug for TxId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let txid_str = self.to_string();
f.debug_tuple("TxId").field(&txid_str).finish()
}
}
impl fmt::Display for TxId {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&ReverseHex::encode(&self.0))
}
}
impl AsRef<[u8; 32]> for TxId {
fn as_ref(&self) -> &[u8; 32] {
&self.0
}
}
impl From<TxId> for [u8; 32] {
fn from(value: TxId) -> Self {
value.0
}
}
impl TxId {
pub const NULL: TxId = TxId([0u8; 32]);
pub const fn from_bytes(bytes: [u8; 32]) -> Self {
TxId(bytes)
}
pub fn read<R: Read>(mut reader: R) -> io::Result<Self> {
let mut hash = [0u8; 32];
reader.read_exact(&mut hash)?;
Ok(TxId::from_bytes(hash))
}
pub fn write<W: Write>(&self, mut writer: W) -> io::Result<()> {
writer.write_all(&self.0)?;
Ok(())
}
pub fn is_null(&self) -> bool {
*self == Self::NULL
}
}