pub struct Bytes<'a>(pub &'a [u8]);Expand description
Display given bytes, if possible, by printing the ascii characters through ~ (except
\) as is, and escaping '\r', '\n', '\t', and '\\'.
If other characters are present everything is printed hexadecimal.
Intended to be used with precision to limit the size of the printed string.
use pubhubs::misc::fmt_ext;
// use precision to limit size to 9 characters
assert_eq!(format!("{:.9}", fmt_ext::Bytes(b"1234567890")).as_str(), "123456...");
// hex is used if any irregular bytes are found
assert_eq!(format!("{}", fmt_ext::Bytes(b"\0 <- zero")).as_str(), "00203c2d207a65726f");
// tabs and newlines are `\`es are escaped
assert_eq!(format!("{}", fmt_ext::Bytes(b"\t\r\n\\")).as_str(), "\\t\\r\\n\\\\");
// otherwise the bytes are left unchanged:
assert_eq!(format!("{}", fmt_ext::Bytes((b' '..=b'~').collect::<Vec<u8>>().as_slice())).as_str(),
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~");Tuple Fields§
§0: &'a [u8]Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Bytes<'a>
impl<'a> RefUnwindSafe for Bytes<'a>
impl<'a> Send for Bytes<'a>
impl<'a> Sync for Bytes<'a>
impl<'a> Unpin for Bytes<'a>
impl<'a> UnsafeUnpin for Bytes<'a>
impl<'a> UnwindSafe for Bytes<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.