Skip to content
Snippets Groups Projects
Commit b1c17169 authored by nanpuyue's avatar nanpuyue Committed by Douman
Browse files

impl `LowerHex`/`UpperHex` for `Bytes`/`BytesMut`

parent 234d8141
No related branches found
No related tags found
No related merge requests found
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);
......@@ -80,6 +80,7 @@ pub use crate::buf::{
mod bytes;
mod debug;
mod hex;
pub use crate::bytes::{Bytes, BytesMut};
// Optional Serde support
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment