Struct UninitSlice
struct UninitSlice(_)
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
UninitSliceis undefined behavior. - Writing uninitialized bytes to an
UninitSliceis 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
impl UninitSlice
fn new(slice: &mut [u8]) -> &mut UninitSliceCreates a
&mut UninitSlicewrapping a slice of initialised memory.Examples
use UninitSlice; let mut buffer = ; let slice = new;fn uninit(slice: &mut [MaybeUninit<u8>]) -> &mut UninitSliceCreates a
&mut UninitSlicewrapping a slice of uninitialised memory.Examples
use UninitSlice; use MaybeUninit; let mut buffer = ; let slice = uninit; let mut vec = Vecwith_capacity; let spare: &mut UninitSlice = vec.spare_capacity_mut.into;unsafe fn from_raw_parts_mut<'a>(ptr: *mut u8, len: usize) -> &'a mut UninitSliceCreate a
&mut UninitSlicefrom a pointer and a length.Safety
The caller must ensure that
ptrreferences a valid memory region owned by the caller representing a byte slice for the duration of'a.Examples
use UninitSlice; let bytes = b"hello world".to_vec; let ptr = bytes.as_ptr as *mut _; let len = bytes.len; let slice = unsafe ;fn write_byte(self: &mut Self, index: usize, byte: u8)Write a single byte at the specified offset.
Panics
The function panics if
indexis out of bounds.Examples
use UninitSlice; let mut data = ; let slice = unsafe ; slice.write_byte; assert_eq!;fn copy_from_slice(self: &mut Self, src: &[u8])Copies bytes from
srcintoself.The length of
srcmust be the same asself.Panics
The function panics if
srchas a different length thanself.Examples
use UninitSlice; let mut data = ; let slice = unsafe ; slice.copy_from_slice; assert_eq!;fn as_mut_ptr(self: &mut Self) -> *mut u8Return 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 BufMut; let mut data = ; let mut slice = &mut data; let ptr = chunk_mut.as_mut_ptr;unsafe fn as_uninit_slice_mut(self: &mut Self) -> &mut [MaybeUninit<u8>]Return a
&mut [MaybeUninit<u8>]to this slice's buffer.Safety
The caller must not read from the referenced memory and must not write uninitialized bytes to the slice either. This is because
BufMutimplementation that created theUninitSliceknows which parts are initialized. Writing uninitialized bytes to the slice may cause theBufMutto read those bytes and trigger undefined behavior.Examples
use BufMut; let mut data = ; let mut slice = &mut data; unsafe ;fn len(self: &Self) -> usizeReturns the number of bytes in the slice.
Examples
use BufMut; let mut data = ; let mut slice = &mut data; let len = chunk_mut.len; assert_eq!;
impl Debug for UninitSlice
fn fmt(self: &Self, fmt: &mut Formatter<'_>) -> Result
impl Freeze for UninitSlice
impl Index for UninitSlice
fn index(self: &Self, index: RangeFrom<usize>) -> &UninitSlice
impl Index for UninitSlice
fn index(self: &Self, index: RangeInclusive<usize>) -> &UninitSlice
impl Index for UninitSlice
fn index(self: &Self, index: RangeToInclusive<usize>) -> &UninitSlice
impl Index for UninitSlice
fn index(self: &Self, index: RangeFull) -> &UninitSlice
impl Index for UninitSlice
fn index(self: &Self, index: RangeTo<usize>) -> &UninitSlice
impl Index for UninitSlice
fn index(self: &Self, index: Range<usize>) -> &UninitSlice
impl IndexMut for UninitSlice
fn index_mut(self: &mut Self, index: RangeFrom<usize>) -> &mut UninitSlice
impl IndexMut for UninitSlice
fn index_mut(self: &mut Self, index: RangeInclusive<usize>) -> &mut UninitSlice
impl IndexMut for UninitSlice
fn index_mut(self: &mut Self, index: RangeToInclusive<usize>) -> &mut UninitSlice
impl IndexMut for UninitSlice
fn index_mut(self: &mut Self, index: Range<usize>) -> &mut UninitSlice
impl IndexMut for UninitSlice
fn index_mut(self: &mut Self, index: RangeFull) -> &mut UninitSlice
impl IndexMut for UninitSlice
fn index_mut(self: &mut Self, index: RangeTo<usize>) -> &mut UninitSlice
impl RefUnwindSafe for UninitSlice
impl Send for UninitSlice
impl Sized for UninitSlice
impl Sync for UninitSlice
impl Unpin for UninitSlice
impl UnsafeUnpin for UninitSlice
impl UnwindSafe for UninitSlice
impl<T> Any for UninitSlice
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for UninitSlice
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for UninitSlice
fn borrow_mut(self: &mut Self) -> &mut T