Bytes

Struct Bytes 

Source
pub struct Bytes<'a>(pub &'a [u8]);
Expand description

A wrapper for bytes slice which tries to guess best way to format it.

If the slice contains printable ASCII characters only, it’s represented as a string surrounded by single quotes (as a consequence, empty value is converted to pair of single quotes). Otherwise, it converts the value into base64.

The intended usage for this type is when trying to format binary data whose structure isn’t known to the caller. For example, when generating debugging or tracing data at database layer where everything is just slices of bytes. At higher levels of abstractions, if the structure of the data is known, it’s usually better to format data in a way that makes sense for the given type.

The type can be used as with tracing::info! and similar calls. For example:

tracing::trace!(target: "state",
                db_op = "insert",
                key = %near_fmt::Bytes(key),
                size = value.len())

See also StorageKey which tries to guess if the data is not a crypto hash.

Tuple Fields§

§0: &'a [u8]

Implementations§

Source§

impl<'a> Bytes<'a>

Source

pub fn from_str(s: &str) -> Result<Vec<u8>, Box<dyn Error + Send + Sync>>

Reverses bytes_format to allow decoding Bytes written with Display.

This looks similar to FromStr but due to lifetime constraints on input and output, the trait cannot be implemented.

Error: Returns an error when the input does not look like an output from bytes_format.

Trait Implementations§

Source§

impl<'a> Debug for Bytes<'a>

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Display for Bytes<'a>

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> UnwindSafe for Bytes<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.