| 1 | use core::fmt::{Formatter, LowerHex, Result, UpperHex}; |
| 2 | |
| 3 | use super::BytesRef; |
| 4 | use crate::{Bytes, BytesMut}; |
| 5 | |
| 6 | impl 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 | |
| 15 | impl 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 | |
| 24 | macro_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 | |
| 34 | hex_impl!(LowerHex, Bytes); |
| 35 | hex_impl!(LowerHex, BytesMut); |
| 36 | hex_impl!(UpperHex, Bytes); |
| 37 | hex_impl!(UpperHex, BytesMut); |
| 38 | |