1use alloc::boxed::Box;
4use core::fmt;
5
6use crate::traits::Hex;
7
8wrapper_lite::wrapper!(
9 #[wrapper(Debug)]
10 #[wrapper(AsRef)]
11 #[wrapper([const] AsMut)]
12 #[wrapper(BorrowMut)]
13 #[wrapper(DerefMut)]
14 #[wrapper(From)]
15 #[bound(T: AsRef<[u8]>)]
16 #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
17 #[derive(Clone, Copy, PartialEq, Eq)]
18 pub struct Display<T> {
31 value: T,
32 }
33);
34
35impl<T: AsRef<[u8]>> fmt::Display for Display<T> {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 fmt::LowerHex::fmt(self, f)
38 }
39}
40
41impl<T: AsRef<[u8]>> fmt::LowerHex for Display<T> {
42 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43 self.write::<false>(f)
44 }
45}
46
47impl<T: AsRef<[u8]>> fmt::UpperHex for Display<T> {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 self.write::<true>(f)
50 }
51}
52
53impl<T> Display<T>
54where
55 T: AsRef<[u8]>,
56{
57 fn write<const UPPER: bool>(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 if f.alternate() {
59 f.write_str("0x")?;
60 }
61
62 f.write_str(
63 &self
64 .encode_hex::<Box<str>, UPPER>()
65 .map_err(|_| fmt::Error)?,
66 )
67 }
68}