#[cfg(feature = "blake3")]
mod impl_blake3;
mod impl_convert;
mod impl_ops;
pub type H160Hash = [u8; 20];
#[derive(Clone, Copy, Default, Eq, Hash, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(default, transparent)
)]
#[repr(transparent)]
pub struct H160(pub H160Hash);
impl H160 {
pub fn from_digest(digest: impl AsRef<[u8]>) -> Self {
let hash = crate::digest_to_hash::<20>(digest);
Self(hash)
}
#[cfg(feature = "blake3")]
pub fn b3(data: impl AsRef<[u8]>) -> Self {
let hash = blake3::hash(data.as_ref());
Self::from_digest(hash.as_bytes())
}
#[cfg(feature = "rand")]
pub fn random() -> Self {
let data = rand::random::<H160Hash>();
let mut raw_bytes = [0; 20];
raw_bytes.copy_from_slice(&data);
(&raw_bytes).into()
}
pub const fn as_slice(&self) -> &[u8] {
&self.0
}
pub fn as_slice_mut(&mut self) -> &mut [u8] {
&mut self.0
}
pub fn copy_from_slice(&mut self, value: &[u8]) -> &mut Self {
self.0.copy_from_slice(value);
self
}
pub const fn get(&self) -> &H160Hash {
&self.0
}
pub const fn get_mut(&mut self) -> &mut H160Hash {
&mut self.0
}
pub fn set(&mut self, value: H160Hash) -> &mut Self {
*self.get_mut() = value;
self
}
pub fn replace(&mut self, value: &H160Hash) -> H160Hash {
core::mem::replace(&mut self.0, *value)
}
pub fn swap(&mut self, other: &mut Self) {
core::mem::swap(&mut self.0, &mut other.0)
}
#[cfg(feature = "alloc")]
pub fn to_vec(&self) -> alloc::vec::Vec<u8> {
self.get().to_vec()
}
}
impl core::fmt::Debug for H160 {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(
f,
"{:>02x}{:>02x}..{:>02x}{:>02x}",
&self[0], &self[1], &self[18], &self[19]
)
}
}
impl core::fmt::Display for H160 {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let start = if let Some(precision) = f.precision() {
if precision >= 64 {
0
} else {
20 - precision / 2
}
} else {
0
};
for byte_idx in start..20 {
write!(f, "{:>02x}", &self.0[byte_idx])?;
}
Ok(())
}
}
impl AsRef<[u8]> for H160 {
#[inline]
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl AsMut<[u8]> for H160 {
#[inline]
fn as_mut(&mut self) -> &mut [u8] {
&mut self.0
}
}
impl core::borrow::Borrow<[u8]> for H160 {
#[inline]
fn borrow(&self) -> &[u8] {
&self.0
}
}
impl core::borrow::BorrowMut<[u8]> for H160 {
#[inline]
fn borrow_mut(&mut self) -> &mut [u8] {
&mut self.0
}
}
impl core::ops::Deref for H160 {
type Target = [u8; 20];
#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl core::ops::DerefMut for H160 {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}