Struct bytes::buf::UninitSlice [−][src]
#[repr(transparent)]pub struct UninitSlice(_);
Expand description
Uninitialized byte slice.
Returned by BufMut::chunk_mut()
, the referenced byte slice may be
uninitialized. The wrapper provides safe access without introducing
undefined behavior.
The safety invariants of this wrapper are:
- Reading from an
UninitSlice
is undefined behavior. - Writing uninitialized bytes to an
UninitSlice
is undefined behavior.
The difference between &mut UninitSlice
and &mut [MaybeUninit<u8>]
is
that it is possible in safe code to write uninitialized bytes to an
&mut [MaybeUninit<u8>]
, which this type prohibits.
Implementations
Create a &mut UninitSlice
from a pointer and a length.
Safety
The caller must ensure that ptr
references a valid memory region owned
by the caller representing a byte slice for the duration of 'a
.
Examples
use bytes::buf::UninitSlice;
let bytes = b"hello world".to_vec();
let ptr = bytes.as_ptr() as *mut _;
let len = bytes.len();
let slice = unsafe { UninitSlice::from_raw_parts_mut(ptr, len) };
Write a single byte at the specified offset.
Panics
The function panics if index
is out of bounds.
Examples
use bytes::buf::UninitSlice;
let mut data = [b'f', b'o', b'o'];
let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) };
slice.write_byte(0, b'b');
assert_eq!(b"boo", &data[..]);
Copies bytes from src
into self
.
The length of src
must be the same as self
.
Panics
The function panics if src
has a different length than self
.
Examples
use bytes::buf::UninitSlice;
let mut data = [b'f', b'o', b'o'];
let slice = unsafe { UninitSlice::from_raw_parts_mut(data.as_mut_ptr(), 3) };
slice.copy_from_slice(b"bar");
assert_eq!(b"bar", &data[..]);
Return a raw pointer to the slice’s buffer.
Safety
The caller must not read from the referenced memory and must not write uninitialized bytes to the slice either.
Examples
use bytes::BufMut;
let mut data = [0, 1, 2];
let mut slice = &mut data[..];
let ptr = BufMut::chunk_mut(&mut slice).as_mut_ptr();
Trait Implementations
type Output = UninitSlice
type Output = UninitSlice
The returned type after indexing.
type Output = UninitSlice
type Output = UninitSlice
The returned type after indexing.
type Output = UninitSlice
type Output = UninitSlice
The returned type after indexing.
Performs the indexing (container[index]
) operation. Read more
type Output = UninitSlice
type Output = UninitSlice
The returned type after indexing.
Performs the indexing (container[index]
) operation. Read more
type Output = UninitSlice
type Output = UninitSlice
The returned type after indexing.
type Output = UninitSlice
type Output = UninitSlice
The returned type after indexing.
Performs the indexing (container[index]
) operation. Read more
Performs the mutable indexing (container[index]
) operation. Read more
Performs the mutable indexing (container[index]
) operation. Read more
Performs the mutable indexing (container[index]
) operation. Read more