Struct valence::protocol::bytes::buf::UninitSlice

pub struct UninitSlice(/* private fields */);
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:

  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

pub 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[..]);

pub 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();

pub 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) };

pub fn write_byte(&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[..]);

pub fn copy_from_slice(&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[..]);

pub fn as_mut_ptr(&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();

pub unsafe fn as_uninit_slice_mut(&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();
};

pub fn len(&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);

Trait Implementations§

§

impl Debug for UninitSlice

§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

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

§

fn from(slice: &'a mut [MaybeUninit<u8>]) -> &'a mut UninitSlice

Converts to this type from the input type.
§

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

§

fn from(slice: &'a mut [u8]) -> &'a mut UninitSlice

Converts to this type from the input type.
§

impl Index<Range<usize>> for UninitSlice

§

type Output = UninitSlice

The returned type after indexing.
§

fn index(&self, index: Range<usize>) -> &UninitSlice

Performs the indexing (container[index]) operation. Read more
§

impl Index<RangeFrom<usize>> for UninitSlice

§

type Output = UninitSlice

The returned type after indexing.
§

fn index(&self, index: RangeFrom<usize>) -> &UninitSlice

Performs the indexing (container[index]) operation. Read more
§

impl Index<RangeFull> for UninitSlice

§

type Output = UninitSlice

The returned type after indexing.
§

fn index(&self, index: RangeFull) -> &UninitSlice

Performs the indexing (container[index]) operation. Read more
§

impl Index<RangeInclusive<usize>> for UninitSlice

§

type Output = UninitSlice

The returned type after indexing.
§

fn index(&self, index: RangeInclusive<usize>) -> &UninitSlice

Performs the indexing (container[index]) operation. Read more
§

impl Index<RangeTo<usize>> for UninitSlice

§

type Output = UninitSlice

The returned type after indexing.
§

fn index(&self, index: RangeTo<usize>) -> &UninitSlice

Performs the indexing (container[index]) operation. Read more
§

impl Index<RangeToInclusive<usize>> for UninitSlice

§

type Output = UninitSlice

The returned type after indexing.
§

fn index(&self, index: RangeToInclusive<usize>) -> &UninitSlice

Performs the indexing (container[index]) operation. Read more
§

impl IndexMut<Range<usize>> for UninitSlice

§

fn index_mut(&mut self, index: Range<usize>) -> &mut UninitSlice

Performs the mutable indexing (container[index]) operation. Read more
§

impl IndexMut<RangeFrom<usize>> for UninitSlice

§

fn index_mut(&mut self, index: RangeFrom<usize>) -> &mut UninitSlice

Performs the mutable indexing (container[index]) operation. Read more
§

impl IndexMut<RangeFull> for UninitSlice

§

fn index_mut(&mut self, index: RangeFull) -> &mut UninitSlice

Performs the mutable indexing (container[index]) operation. Read more
§

impl IndexMut<RangeInclusive<usize>> for UninitSlice

§

fn index_mut(&mut self, index: RangeInclusive<usize>) -> &mut UninitSlice

Performs the mutable indexing (container[index]) operation. Read more
§

impl IndexMut<RangeTo<usize>> for UninitSlice

§

fn index_mut(&mut self, index: RangeTo<usize>) -> &mut UninitSlice

Performs the mutable indexing (container[index]) operation. Read more
§

impl IndexMut<RangeToInclusive<usize>> for UninitSlice

§

fn index_mut(&mut self, index: RangeToInclusive<usize>) -> &mut UninitSlice

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Pipe for T
where T: ?Sized,

§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.