Struct 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:
- 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
impl UninitSlice
pub fn new(slice: &mut [u8]) -> &mut UninitSlice
Available on crate features http and grpc and protobuf only.
pub fn new(slice: &mut [u8]) -> &mut UninitSlice
http and grpc and protobuf only.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
Available on crate features http and grpc and protobuf only.
pub fn uninit(slice: &mut [MaybeUninit<u8>]) -> &mut UninitSlice
http and grpc and protobuf only.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
Available on crate features http and grpc and protobuf only.
pub unsafe fn from_raw_parts_mut<'a>( ptr: *mut u8, len: usize, ) -> &'a mut UninitSlice
http and grpc and protobuf only.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)
Available on crate features http and grpc and protobuf only.
pub fn write_byte(&mut self, index: usize, byte: u8)
http and grpc and protobuf only.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])
Available on crate features http and grpc and protobuf only.
pub fn copy_from_slice(&mut self, src: &[u8])
http and grpc and protobuf only.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
Available on crate features http and grpc and protobuf only.
pub fn as_mut_ptr(&mut self) -> *mut u8
http and grpc and protobuf only.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>]
Available on crate features http and grpc and protobuf only.
pub unsafe fn as_uninit_slice_mut(&mut self) -> &mut [MaybeUninit<u8>]
http and grpc and protobuf only.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();
};Trait Implementations§
§impl Debug for UninitSlice
impl Debug for UninitSlice
§impl<'a> From<&'a mut [MaybeUninit<u8>]> for &'a mut UninitSlice
impl<'a> From<&'a mut [MaybeUninit<u8>]> for &'a mut UninitSlice
§fn from(slice: &'a mut [MaybeUninit<u8>]) -> &'a mut UninitSlice
fn from(slice: &'a mut [MaybeUninit<u8>]) -> &'a mut UninitSlice
§impl<'a> From<&'a mut [u8]> for &'a mut UninitSlice
impl<'a> From<&'a mut [u8]> for &'a mut UninitSlice
§fn from(slice: &'a mut [u8]) -> &'a mut UninitSlice
fn from(slice: &'a mut [u8]) -> &'a mut UninitSlice
§impl Index<Range<usize>> for UninitSlice
impl Index<Range<usize>> for UninitSlice
§type Output = UninitSlice
type Output = UninitSlice
§impl Index<RangeFrom<usize>> for UninitSlice
impl Index<RangeFrom<usize>> for UninitSlice
§type Output = UninitSlice
type Output = UninitSlice
§impl Index<RangeFull> for UninitSlice
impl Index<RangeFull> for UninitSlice
§type Output = UninitSlice
type Output = UninitSlice
§fn index(&self, index: RangeFull) -> &UninitSlice
fn index(&self, index: RangeFull) -> &UninitSlice
container[index]) operation. Read more§impl Index<RangeInclusive<usize>> for UninitSlice
impl Index<RangeInclusive<usize>> for UninitSlice
§type Output = UninitSlice
type Output = UninitSlice
§fn index(&self, index: RangeInclusive<usize>) -> &UninitSlice
fn index(&self, index: RangeInclusive<usize>) -> &UninitSlice
container[index]) operation. Read more§impl Index<RangeTo<usize>> for UninitSlice
impl Index<RangeTo<usize>> for UninitSlice
§type Output = UninitSlice
type Output = UninitSlice
§impl Index<RangeToInclusive<usize>> for UninitSlice
impl Index<RangeToInclusive<usize>> for UninitSlice
§type Output = UninitSlice
type Output = UninitSlice
§fn index(&self, index: RangeToInclusive<usize>) -> &UninitSlice
fn index(&self, index: RangeToInclusive<usize>) -> &UninitSlice
container[index]) operation. Read more§impl IndexMut<Range<usize>> for UninitSlice
impl IndexMut<Range<usize>> for UninitSlice
§impl IndexMut<RangeFrom<usize>> for UninitSlice
impl IndexMut<RangeFrom<usize>> for UninitSlice
§impl IndexMut<RangeFull> for UninitSlice
impl IndexMut<RangeFull> for UninitSlice
§fn index_mut(&mut self, index: RangeFull) -> &mut UninitSlice
fn index_mut(&mut self, index: RangeFull) -> &mut UninitSlice
container[index]) operation. Read more§impl IndexMut<RangeInclusive<usize>> for UninitSlice
impl IndexMut<RangeInclusive<usize>> for UninitSlice
§fn index_mut(&mut self, index: RangeInclusive<usize>) -> &mut UninitSlice
fn index_mut(&mut self, index: RangeInclusive<usize>) -> &mut UninitSlice
container[index]) operation. Read more§impl IndexMut<RangeTo<usize>> for UninitSlice
impl IndexMut<RangeTo<usize>> for UninitSlice
§impl IndexMut<RangeToInclusive<usize>> for UninitSlice
impl IndexMut<RangeToInclusive<usize>> for UninitSlice
§fn index_mut(&mut self, index: RangeToInclusive<usize>) -> &mut UninitSlice
fn index_mut(&mut self, index: RangeToInclusive<usize>) -> &mut UninitSlice
container[index]) operation. Read moreAuto Trait Implementations§
impl Freeze for 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
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<P, B, E>(self, other: P) -> And<T, P>
fn and<P, B, E>(self, other: P) -> And<T, P>
Policy that returns Action::Follow only if self and other return
Action::Follow. Read more§impl<V, F> ValueFormatter<&V> for F
impl<V, F> ValueFormatter<&V> for F
§fn format_value(writer: impl ValueWriter, value: &&V)
fn format_value(writer: impl ValueWriter, value: &&V)
value to writer§impl<V, F> ValueFormatter<Arc<V>> for F
impl<V, F> ValueFormatter<Arc<V>> for F
§fn format_value(writer: impl ValueWriter, value: &Arc<V>)
fn format_value(writer: impl ValueWriter, value: &Arc<V>)
value to writer§impl<V, F> ValueFormatter<Box<V>> for F
impl<V, F> ValueFormatter<Box<V>> for F
§fn format_value(writer: impl ValueWriter, value: &Box<V>)
fn format_value(writer: impl ValueWriter, value: &Box<V>)
value to writer§impl<V, F> ValueFormatter<Cow<'_, V>> for F
impl<V, F> ValueFormatter<Cow<'_, V>> for F
§fn format_value(writer: impl ValueWriter, value: &Cow<'_, V>)
fn format_value(writer: impl ValueWriter, value: &Cow<'_, V>)
value to writer