bytes/fmt/
hex.rs

1use core::fmt::{Formatter, LowerHex, Result, UpperHex};
2
3use super::BytesRef;
4use crate::{Bytes, BytesMut};
5
6impl LowerHex for BytesRef<'_> {
7    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
8        for &b in self.0 {
9            write!(f, "{:02x}", b)?;
10        }
11        Ok(())
12    }
13}
14
15impl UpperHex for BytesRef<'_> {
16    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
17        for &b in self.0 {
18            write!(f, "{:02X}", b)?;
19        }
20        Ok(())
21    }
22}
23
24macro_rules! hex_impl {
25    ($tr:ident, $ty:ty) => {
26        impl $tr for $ty {
27            fn fmt(&self, f: &mut Formatter<'_>) -> Result {
28                $tr::fmt(&BytesRef(self.as_ref()), f)
29            }
30        }
31    };
32}
33
34hex_impl!(LowerHex, Bytes);
35hex_impl!(LowerHex, BytesMut);
36hex_impl!(UpperHex, Bytes);
37hex_impl!(UpperHex, BytesMut);