diff options
Diffstat (limited to '')
-rw-r--r-- | third_party/rust/bytes/src/hex.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/third_party/rust/bytes/src/hex.rs b/third_party/rust/bytes/src/hex.rs new file mode 100644 index 0000000000..48ae6a42c1 --- /dev/null +++ b/third_party/rust/bytes/src/hex.rs @@ -0,0 +1,37 @@ +use crate::{Bytes, BytesMut}; +use core::fmt::{Formatter, LowerHex, Result, UpperHex}; + +struct BytesRef<'a>(&'a [u8]); + +impl<'a> LowerHex for BytesRef<'a> { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + for b in self.0 { + write!(f, "{:02x}", b)?; + } + Ok(()) + } +} + +impl<'a> UpperHex for BytesRef<'a> { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + for b in self.0 { + write!(f, "{:02X}", b)?; + } + Ok(()) + } +} + +macro_rules! hex_impl { + ($tr:ident, $ty:ty) => { + impl $tr for $ty { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + $tr::fmt(&BytesRef(self.as_ref()), f) + } + } + }; +} + +hex_impl!(LowerHex, Bytes); +hex_impl!(LowerHex, BytesMut); +hex_impl!(UpperHex, Bytes); +hex_impl!(UpperHex, BytesMut); |