static_bytes - Rust (original) (raw)

Expand description

The aim of this crate is to improve user experience when working with static bytes. Look at this pseudo code example to understand problem with &mut [u8] and bytes::buf::BufMut

let mut fixed_storage = [u8; 16];
let mut slice = fixed_storage[..];
let len_before = slice.len();
// declaration fn encode(&self, buf: &mut dyn BufMut);
frame.encode(&mut slice);
let len = len_before - slice.len();
let filled_bytes = fixed_storage[..len];

There are two problems with code above:

You can resolve both with SafeBytesSlice. For example usage seedocs.

§Compatibility with bytes

error

When things go wrong.

SafeBytesSlice

Non panic wrapper for &mut [u8] that implement BufMut trait.