diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bytes.rs | 7 | ||||
-rw-r--r-- | src/bytes_mut.rs | 7 | ||||
-rw-r--r-- | src/debug.rs | 40 | ||||
-rw-r--r-- | src/fmt/debug.rs | 49 | ||||
-rw-r--r-- | src/fmt/hex.rs (renamed from src/hex.rs) | 12 | ||||
-rw-r--r-- | src/fmt/mod.rs | 5 | ||||
-rw-r--r-- | src/lib.rs | 3 |
7 files changed, 61 insertions, 62 deletions
diff --git a/src/bytes.rs b/src/bytes.rs index 0ca6138..d70852a 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -6,7 +6,6 @@ use alloc::{vec::Vec, string::String, boxed::Box, borrow::Borrow}; use crate::Buf; use crate::buf::IntoIter; -use crate::debug; use crate::loom::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering}; /// A reference counted contiguous slice of memory. @@ -496,12 +495,6 @@ impl Clone for Bytes { } } -impl fmt::Debug for Bytes { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(&debug::BsDebug(&self.as_slice()), f) - } -} - impl Buf for Bytes { #[inline] fn remaining(&self) -> usize { diff --git a/src/bytes_mut.rs b/src/bytes_mut.rs index c3131a3..dc4e4b1 100644 --- a/src/bytes_mut.rs +++ b/src/bytes_mut.rs @@ -9,7 +9,6 @@ use alloc::{vec::Vec, string::String, boxed::Box, borrow::{Borrow, BorrowMut}}; use crate::{Bytes, Buf, BufMut}; use crate::bytes::Vtable; use crate::buf::IntoIter; -use crate::debug; use crate::loom::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering}; /// A unique reference to a contiguous slice of memory. @@ -1074,12 +1073,6 @@ impl Default for BytesMut { } } -impl fmt::Debug for BytesMut { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(&debug::BsDebug(&self.as_slice()), fmt) - } -} - impl hash::Hash for BytesMut { fn hash<H>(&self, state: &mut H) where H: hash::Hasher { let s: &[u8] = self.as_ref(); diff --git a/src/debug.rs b/src/debug.rs deleted file mode 100644 index b1a3cc8..0000000 --- a/src/debug.rs +++ /dev/null @@ -1,40 +0,0 @@ -use core::fmt; - -/// Alternative implementation of `fmt::Debug` for byte slice. -/// -/// Standard `Debug` implementation for `[u8]` is comma separated -/// list of numbers. Since large amount of byte strings are in fact -/// ASCII strings or contain a lot of ASCII strings (e. g. HTTP), -/// it is convenient to print strings as ASCII when possible. -/// -/// This struct wraps `&[u8]` just to override `fmt::Debug`. -/// -/// `BsDebug` is not a part of public API of bytes crate. -pub struct BsDebug<'a>(pub &'a [u8]); - -impl fmt::Debug for BsDebug<'_> { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - write!(fmt, "b\"")?; - for &c in self.0 { - // https://doc.rust-lang.org/reference.html#byte-escapes - if c == b'\n' { - write!(fmt, "\\n")?; - } else if c == b'\r' { - write!(fmt, "\\r")?; - } else if c == b'\t' { - write!(fmt, "\\t")?; - } else if c == b'\\' || c == b'"' { - write!(fmt, "\\{}", c as char)?; - } else if c == b'\0' { - write!(fmt, "\\0")?; - // ASCII printable - } else if c >= 0x20 && c < 0x7f { - write!(fmt, "{}", c as char)?; - } else { - write!(fmt, "\\x{:02x}", c)?; - } - } - write!(fmt, "\"")?; - Ok(()) - } -} diff --git a/src/fmt/debug.rs b/src/fmt/debug.rs new file mode 100644 index 0000000..f6a08b8 --- /dev/null +++ b/src/fmt/debug.rs @@ -0,0 +1,49 @@ +use core::fmt::{Debug, Formatter, Result}; + +use crate::{Bytes, BytesMut}; +use super::BytesRef; + +/// Alternative implementation of `std::fmt::Debug` for byte slice. +/// +/// Standard `Debug` implementation for `[u8]` is comma separated +/// list of numbers. Since large amount of byte strings are in fact +/// ASCII strings or contain a lot of ASCII strings (e. g. HTTP), +/// it is convenient to print strings as ASCII when possible. +impl Debug for BytesRef<'_> { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + write!(f, "b\"")?; + for &b in self.0 { + // https://doc.rust-lang.org/reference/tokens.html#byte-escapes + if b == b'\n' { + write!(f, "\\n")?; + } else if b == b'\r' { + write!(f, "\\r")?; + } else if b == b'\t' { + write!(f, "\\t")?; + } else if b == b'\\' || b == b'"' { + write!(f, "\\{}", b as char)?; + } else if b == b'\0' { + write!(f, "\\0")?; + // ASCII printable + } else if b >= 0x20 && b < 0x7f { + write!(f, "{}", b as char)?; + } else { + write!(f, "\\x{:02x}", b)?; + } + } + write!(f, "\"")?; + Ok(()) + } +} + +impl Debug for Bytes { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + Debug::fmt(&BytesRef(&self.as_ref()), f) + } +} + +impl Debug for BytesMut { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + Debug::fmt(&BytesRef(&self.as_ref()), f) + } +} diff --git a/src/hex.rs b/src/fmt/hex.rs index 48ae6a4..09170ae 100644 --- a/src/hex.rs +++ b/src/fmt/hex.rs @@ -1,20 +1,20 @@ -use crate::{Bytes, BytesMut}; use core::fmt::{Formatter, LowerHex, Result, UpperHex}; -struct BytesRef<'a>(&'a [u8]); +use crate::{Bytes, BytesMut}; +use super::BytesRef; -impl<'a> LowerHex for BytesRef<'a> { +impl LowerHex for BytesRef<'_> { fn fmt(&self, f: &mut Formatter<'_>) -> Result { - for b in self.0 { + for &b in self.0 { write!(f, "{:02x}", b)?; } Ok(()) } } -impl<'a> UpperHex for BytesRef<'a> { +impl UpperHex for BytesRef<'_> { fn fmt(&self, f: &mut Formatter<'_>) -> Result { - for b in self.0 { + for &b in self.0 { write!(f, "{:02X}", b)?; } Ok(()) diff --git a/src/fmt/mod.rs b/src/fmt/mod.rs new file mode 100644 index 0000000..676d15f --- /dev/null +++ b/src/fmt/mod.rs @@ -0,0 +1,5 @@ +mod debug; +mod hex; + +/// `BytesRef` is not a part of public API of bytes crate. +struct BytesRef<'a>(&'a [u8]); @@ -86,8 +86,7 @@ pub use crate::buf::{ mod bytes_mut; mod bytes; -mod debug; -mod hex; +mod fmt; mod loom; pub use crate::bytes_mut::BytesMut; pub use crate::bytes::Bytes; |