[][src]Struct static_bytes::SafeBytesSlice

pub struct SafeBytesSlice<'a> { /* fields omitted */ }

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

Example

work with uninitialized memory &mut [MaybeUninit<u8>]

use bytes::buf::BufMut;
use static_bytes::SafeBytesSlice;
use core::mem::MaybeUninit;
//are you sure that's random?
fn fill_with_random(buf: &mut dyn BufMut, amount: usize) {
    for _ in 0..amount {
        buf.put_u8(9);
    }
}
// This is optimized way of working with slices
// This is safe. See way in rust doc:
// https://doc.rust-lang.org/std/mem/union.MaybeUninit.html#initializing-an-array-element-by-element
let mut fixed_storage: [MaybeUninit<u8>; 128] = unsafe {
       MaybeUninit::uninit().assume_init()
   };
let mut slice = SafeBytesSlice::from(&mut fixed_storage[..]);
// your function that accept `&mut dyn BufMut`
fill_with_random(&mut slice, 32);
let output = slice.try_into_bytes().unwrap();
assert_eq!(output.len(), 32);
assert_eq!(output[31], 9);

work with standard slice &mut [u8]

use bytes::buf::BufMut;
use static_bytes::SafeBytesSlice;

let mut fixed_storage = [0u8; 64];
let mut slice = SafeBytesSlice::from(&mut fixed_storage[..]);
// your function that accept `&mut dyn BufMut`
// see function impl in `&mut [MaybeUninit<u8>]` example
fill_with_random(&mut slice, 32);
let output = slice.try_into_bytes().unwrap();
assert_eq!(output.len(), 32);
assert_eq!(output[31], 9);

Is fill_with_random() random?

Implementations

impl<'a> SafeBytesSlice<'a>[src]

pub fn try_into_bytes(self) -> Result<&'a [u8], CapacityExceeded>[src]

Returns filled bytes (&[u8]) or error if capacity exceeded.

pub fn is_exceed(&self) -> bool[src]

Returns true if capacity was exceeded.

SafeBytesSlice is not usable anymore - there is no access to inner bytes because they are in improper state.

Trait Implementations

impl<'a> BufMut for SafeBytesSlice<'a>[src]

impl<'a> From<&'a mut [MaybeUninit<u8>]> for SafeBytesSlice<'a>[src]

impl<'a> From<&'a mut [u8]> for SafeBytesSlice<'a>[src]

Auto Trait Implementations

impl<'a> Send for SafeBytesSlice<'a>[src]

impl<'a> Sync for SafeBytesSlice<'a>[src]

impl<'a> Unpin for SafeBytesSlice<'a>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.