use std::fmt;
use std::ops::Deref;
#[derive(Debug, Copy, Clone, Hash)]
pub struct HexSlice<'a>(&'a [u8]);
impl<'a> HexSlice<'a> {
pub fn new<T>(data: &'a T) -> Self
where T: ?Sized + AsRef<[u8]> + 'a {
HexSlice(data.as_ref())
}
pub fn format(&self) -> String { format!("{:x}", self) }
}
impl<'a> Deref for HexSlice<'a> {
type Target = [u8];
fn deref(&self) -> &Self::Target { self.0 }
}
impl<'a> AsRef<[u8]> for HexSlice<'a> {
fn as_ref(&self) -> &[u8] { self.0 }
}
impl<'a> fmt::Display for HexSlice<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:x}", self) }
}
impl<'a> fmt::LowerHex for HexSlice<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for byte in self.0 {
write!(f, "{:02x}", byte)?;
}
Ok(())
}
}
impl<'a> fmt::UpperHex for HexSlice<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for byte in self.0 {
write!(f, "{:02X}", byte)?;
}
Ok(())
}
}