merge hex & debug into fmt (#357)

This commit is contained in:
南浦月
2020-01-23 10:08:40 -08:00
committed by Sean McArthur
parent f9ebf74091
commit 3853a1fac4
7 changed files with 61 additions and 62 deletions
+37
View File
@@ -0,0 +1,37 @@
use core::fmt::{Formatter, LowerHex, Result, UpperHex};
use crate::{Bytes, BytesMut};
use super::BytesRef;
impl LowerHex for BytesRef<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
for &b in self.0 {
write!(f, "{:02x}", b)?;
}
Ok(())
}
}
impl UpperHex for BytesRef<'_> {
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);