Files
bytes/src/fmt/hex.rs
T

38 lines
824 B
Rust
Raw Normal View History

use core::fmt::{Formatter, LowerHex, Result, UpperHex};
2019-07-31 07:50:57 +08:00
2020-01-24 02:08:40 +08:00
use super::BytesRef;
2020-05-22 13:17:30 +09:00
use crate::{Bytes, BytesMut};
2019-07-31 07:50:57 +08:00
2020-01-24 02:08:40 +08:00
impl LowerHex for BytesRef<'_> {
2019-07-31 07:50:57 +08:00
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2020-01-24 02:08:40 +08:00
for &b in self.0 {
2019-07-31 07:50:57 +08:00
write!(f, "{:02x}", b)?;
}
Ok(())
}
}
2020-01-24 02:08:40 +08:00
impl UpperHex for BytesRef<'_> {
2019-07-31 07:50:57 +08:00
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2020-01-24 02:08:40 +08:00
for &b in self.0 {
2019-07-31 07:50:57 +08:00
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);