mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-26 00:00:20 +02:00
merge hex & debug into fmt (#357)
This commit is contained in:
@@ -6,7 +6,6 @@ use alloc::{vec::Vec, string::String, boxed::Box, borrow::Borrow};
|
|||||||
|
|
||||||
use crate::Buf;
|
use crate::Buf;
|
||||||
use crate::buf::IntoIter;
|
use crate::buf::IntoIter;
|
||||||
use crate::debug;
|
|
||||||
use crate::loom::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
|
use crate::loom::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
|
||||||
|
|
||||||
/// A reference counted contiguous slice of memory.
|
/// 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 {
|
impl Buf for Bytes {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn remaining(&self) -> usize {
|
fn remaining(&self) -> usize {
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ use alloc::{vec::Vec, string::String, boxed::Box, borrow::{Borrow, BorrowMut}};
|
|||||||
use crate::{Bytes, Buf, BufMut};
|
use crate::{Bytes, Buf, BufMut};
|
||||||
use crate::bytes::Vtable;
|
use crate::bytes::Vtable;
|
||||||
use crate::buf::IntoIter;
|
use crate::buf::IntoIter;
|
||||||
use crate::debug;
|
|
||||||
use crate::loom::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
|
use crate::loom::sync::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
|
||||||
|
|
||||||
/// A unique reference to a contiguous slice of memory.
|
/// 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 {
|
impl hash::Hash for BytesMut {
|
||||||
fn hash<H>(&self, state: &mut H) where H: hash::Hasher {
|
fn hash<H>(&self, state: &mut H) where H: hash::Hasher {
|
||||||
let s: &[u8] = self.as_ref();
|
let s: &[u8] = self.as_ref();
|
||||||
|
|||||||
@@ -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(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,20 +1,20 @@
|
|||||||
use crate::{Bytes, BytesMut};
|
|
||||||
use core::fmt::{Formatter, LowerHex, Result, UpperHex};
|
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 {
|
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
|
||||||
for b in self.0 {
|
for &b in self.0 {
|
||||||
write!(f, "{:02x}", b)?;
|
write!(f, "{:02x}", b)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> UpperHex for BytesRef<'a> {
|
impl UpperHex for BytesRef<'_> {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
|
||||||
for b in self.0 {
|
for &b in self.0 {
|
||||||
write!(f, "{:02X}", b)?;
|
write!(f, "{:02X}", b)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
mod debug;
|
||||||
|
mod hex;
|
||||||
|
|
||||||
|
/// `BytesRef` is not a part of public API of bytes crate.
|
||||||
|
struct BytesRef<'a>(&'a [u8]);
|
||||||
+1
-2
@@ -86,8 +86,7 @@ pub use crate::buf::{
|
|||||||
|
|
||||||
mod bytes_mut;
|
mod bytes_mut;
|
||||||
mod bytes;
|
mod bytes;
|
||||||
mod debug;
|
mod fmt;
|
||||||
mod hex;
|
|
||||||
mod loom;
|
mod loom;
|
||||||
pub use crate::bytes_mut::BytesMut;
|
pub use crate::bytes_mut::BytesMut;
|
||||||
pub use crate::bytes::Bytes;
|
pub use crate::bytes::Bytes;
|
||||||
|
|||||||
Reference in New Issue
Block a user