impl LowerHex/UpperHex for Bytes/BytesMut

This commit is contained in:
nanpuyue
2019-08-16 15:49:37 +02:00
committed by Douman
parent 234d814122
commit b1c17169d9
2 changed files with 38 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
use crate::{Bytes, BytesMut};
use std::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);
+1
View File
@@ -80,6 +80,7 @@ pub use crate::buf::{
mod bytes;
mod debug;
mod hex;
pub use crate::bytes::{Bytes, BytesMut};
// Optional Serde support