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:

  1. Reading from an UninitSlice is undefined behavior.
  2. 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

impl UninitSlice

fn new(slice: &mut [u8]) -> &mut UninitSlice

Creates a &mut UninitSlice wrapping a slice of initialised memory.

Examples

use bytes::buf::UninitSlice;

let mut buffer = [0u8; 64];
let slice = UninitSlice::new(&mut buffer[..]);
fn uninit(slice: &mut [MaybeUninit<u8>]) -> &mut UninitSlice

Creates a &mut UninitSlice wrapping a slice of uninitialised memory.

Examples

use bytes::buf::UninitSlice;
use core::mem::MaybeUninit;

let mut buffer = [MaybeUninit::uninit(); 64];
let slice = UninitSlice::uninit(&mut buffer[..]);

let mut vec = Vec::with_capacity(1024);
let spare: &mut UninitSlice = vec.spare_capacity_mut().into();
unsafe fn from_raw_parts_mut<'a>(ptr: *mut u8, len: usize) -> &'a mut UninitSlice

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) };
fn write_byte(self: &mut Self, index: usize, byte: u8)

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[..]);
fn copy_from_slice(self: &mut Self, src: &[u8])

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[..]);
fn as_mut_ptr(self: &mut Self) -> *mut u8

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();
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 BufMut implementation that created the UninitSlice knows which parts are initialized. Writing uninitialized bytes to the slice may cause the BufMut to read those bytes and trigger undefined behavior.

Examples

use bytes::BufMut;

let mut data = [0, 1, 2];
let mut slice = &mut data[..];
unsafe {
    let uninit_slice = BufMut::chunk_mut(&mut slice).as_uninit_slice_mut();
};
fn len(self: &Self) -> usize

Returns the number of bytes in the slice.

Examples

use bytes::BufMut;

let mut data = [0, 1, 2];
let mut slice = &mut data[..];
let len = BufMut::chunk_mut(&mut slice).len();

assert_eq!(len, 3);

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