use alloc::vec::Vec;
use core::{fmt, str};
wrapper_lite::wrapper!(
#[wrapper(Debug)]
#[wrapper(AsRef)]
#[wrapper(Deref)]
#[wrapper(From)]
#[bound(T: AsRef<[u8]>)]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Display<T> {
value: T,
}
);
impl<T: AsRef<[u8]>> fmt::Display for Display<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::LowerHex::fmt(self, f)
}
}
impl<T: AsRef<[u8]>> fmt::LowerHex for Display<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.write::<false>(f)
}
}
impl<T: AsRef<[u8]>> fmt::UpperHex for Display<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.write::<true>(f)
}
}
impl<T> Display<T>
where
T: AsRef<[u8]>,
{
fn write<const UPPER: bool>(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
f.write_str("0x")?;
}
let bytes = self.value.as_ref();
let mut buf = Vec::with_capacity(bytes.len() * 2);
let encoded = crate::encode::<_, UPPER>(bytes, &mut buf);
#[allow(unsafe_code, reason = "XXX")]
unsafe {
debug_assert!(encoded.is_ok(), "pre-allocated capacity is sufficient");
let encoded = encoded.unwrap_unchecked();
f.write_str(str::from_utf8_unchecked(encoded))
}
}
}