[−][src]Struct static_bytes::SafeBytesSlice
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]
pub fn remaining_mut(&self) -> usize
[src]
pub unsafe fn advance_mut(&mut self, cnt: usize)
[src]
pub fn chunk_mut(&mut self) -> &mut UninitSlice
[src]
pub fn put_slice(&mut self, src: &[u8])
[src]
pub fn has_remaining_mut(&self) -> bool
[src]
pub fn put<T>(&mut self, src: T) where
T: Buf,
[src]
T: Buf,
pub fn put_u8(&mut self, n: u8)
[src]
pub fn put_i8(&mut self, n: i8)
[src]
pub fn put_u16(&mut self, n: u16)
[src]
pub fn put_u16_le(&mut self, n: u16)
[src]
pub fn put_i16(&mut self, n: i16)
[src]
pub fn put_i16_le(&mut self, n: i16)
[src]
pub fn put_u32(&mut self, n: u32)
[src]
pub fn put_u32_le(&mut self, n: u32)
[src]
pub fn put_i32(&mut self, n: i32)
[src]
pub fn put_i32_le(&mut self, n: i32)
[src]
pub fn put_u64(&mut self, n: u64)
[src]
pub fn put_u64_le(&mut self, n: u64)
[src]
pub fn put_i64(&mut self, n: i64)
[src]
pub fn put_i64_le(&mut self, n: i64)
[src]
pub fn put_u128(&mut self, n: u128)
[src]
pub fn put_u128_le(&mut self, n: u128)
[src]
pub fn put_i128(&mut self, n: i128)
[src]
pub fn put_i128_le(&mut self, n: i128)
[src]
pub fn put_uint(&mut self, n: u64, nbytes: usize)
[src]
pub fn put_uint_le(&mut self, n: u64, nbytes: usize)
[src]
pub fn put_int(&mut self, n: i64, nbytes: usize)
[src]
pub fn put_int_le(&mut self, n: i64, nbytes: usize)
[src]
pub fn put_f32(&mut self, n: f32)
[src]
pub fn put_f32_le(&mut self, n: f32)
[src]
pub fn put_f64(&mut self, n: f64)
[src]
pub fn put_f64_le(&mut self, n: f64)
[src]
pub fn limit(self, limit: usize) -> Limit<Self>
[src]
pub fn chain_mut<U>(self, next: U) -> Chain<Self, U> where
U: BufMut,
[src]
U: BufMut,
impl<'a> From<&'a mut [MaybeUninit<u8>]> for SafeBytesSlice<'a>
[src]
pub fn from(slice: &'a mut [MaybeUninit<u8>]) -> Self
[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]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,