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>
impl<'a> Bytes<'a>
Sourcepub fn from_str(s: &str) -> Result<Vec<u8>, Box<dyn Error + Send + Sync>>
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
.