Struct possibly_uninit::slice::OutSlice [−][src]
A slice of T
that may be uninitialized, but might also be borrowed from
&mut [T]
.
One would normally expect this to be &mut [MaybeUninit<T>]
, however,
that wouldn’t be sound. Consider this code:
use core::mem::MaybeUninit; fn cast_mut<T>(val: &mut [T]) -> &mut [MaybeUninit<T>] { unsafe { core::slice::from_raw_parts_mut(val.as_mut_ptr() as *mut MaybeUninit<T>, val.len()) } } // No unsafe code here fn main() { let mut message = ["Hello world!".to_string()]; core::mem::replace(&mut cast_mut(&mut message)[0], MaybeUninit::uninit()); println!("This is now garbage: {}", message[0]); }
The code above triggers UB. Thus the users of the slice must be prevented from writing invalid values into the slice. That’s only possible by creating a newtype like this one.
While the newtype itself doesn’t track initializedness, so its use may be
limited in safe code, it’s a base building block allowing sound
implementations of wrappers tracking initializedness. See Cursor
type in
this crate.
Implementations
impl<T> OutSlice<T>
[src]
pub fn at<I: SliceIndex<[MaybeUninit<T>], Output = MaybeUninit<T>>>(
&self,
index: I
) -> &MaybeUninit<T>
[src]
&self,
index: I
) -> &MaybeUninit<T>
Accesses the value at given index.
Note that this is useless unless you know that the value is
initialized. Declare so by unsafe
ly calling assume_init_ref
.
pub fn at_mut<I: SliceIndex<[MaybeUninit<T>], Output = MaybeUninit<T>>>(
&mut self,
index: I
) -> Out<'_, T>
[src]
&mut self,
index: I
) -> Out<'_, T>
Accesses value at given index.
A special reference wrapper is returned that allows you to only write valid values.
pub fn len(&self) -> usize
[src]
Returns the length of the slice.
pub fn is_empty(&self) -> bool
[src]
Returns true
if the slice is empty.
pub fn iter_mut(&mut self) -> IterMut<'_, T>ⓘ
[src]
Returns an iterator that allows initializing the slice
pub fn split_at_mut(
&mut self,
mid: usize
) -> (&mut OutSlice<T>, &mut OutSlice<T>)
[src]
&mut self,
mid: usize
) -> (&mut OutSlice<T>, &mut OutSlice<T>)
pub unsafe fn assume_init(&self) -> &[T]
[src]
Transforms the slice into initialized version.
Safety
This method is unsafe
because it may only be called when the whole
slice was initialized. Use indexing operator to get a subslice if only a
part of the slice was initialized.
pub unsafe fn assume_init_mut(&mut self) -> &mut [T]
[src]
Transforms the slice into initialized mutable version.
Safety
This method is unsafe
because it may only be called when the whole
slice was initialized. Use indexing operator to get a subslice if only a
part of the slice was initialized.
pub fn as_non_null(&mut self) -> NonNull<T>
[src]
Returns non-null pointer to the first element of the slice.
pub fn as_mut_ptr(&mut self) -> *mut T
[src]
Returns mutable raw pointer to the first element of the slice.
Note that this is in fact non-null, it’s just sometimes more useful than
as_non_null
.
pub fn write_zeroes(&mut self) -> &mut [T] where
T: ZeroValid,
[src]
T: ZeroValid,
Initializes the whole slice by overwriting it with zeroes.
This is only possible if Item
allows safely initializing itself with
zeroes. (e.g. in case of integer types)
You may implement ZeroValid
if zero bit pattern is valid for your
type.
pub fn init_from_iter<I>(&mut self, iter: I) -> &mut [T] where
I: IntoIterator,
I::Item: TakeItem<T>,
[src]
I: IntoIterator,
I::Item: TakeItem<T>,
Uses the iterator to initialize the slice
pub fn copy_from_slice(&mut self, slice: &[T]) -> &mut [T] where
T: Copy,
[src]
T: Copy,
Initializes the slice by copying another slice into it.
This is a specialized version of init_from_iter
, which may be more
performant.
Panics
The method panics if the slices are of different lengths. Use indexing opertors to make the lengths same.
Trait Implementations
impl<'a, T> BorrowOutSlice<T> for OutSlice<T>
[src]
fn borrow_out_slice(&mut self) -> &mut OutSlice<T>
[src]
fn zero_if_needed(&mut self) -> &mut [Item] where
Item: ZeroValid,
[src]
Item: ZeroValid,
fn init_with_copy_from_slice(&mut self, slice: &[Item]) -> &mut [Item] where
Item: Copy,
[src]
Item: Copy,
fn init_with_copy_from_slice_min(&mut self, slice: &[Item]) -> &mut [Item] where
Item: Copy,
[src]
Item: Copy,
unsafe fn assume_init_mut(&mut self) -> &mut [Item]
[src]
impl<T> BorrowUninitSlice<T> for OutSlice<T>
[src]
fn borrow_uninit_slice(&self) -> &[MaybeUninit<T>]
[src]
unsafe fn assume_init(&self) -> &[Item]
[src]
impl<'a, T> From<&'a [MaybeUninit<T>]> for &'a OutSlice<T>
[src]
fn from(value: &'a [MaybeUninit<T>]) -> Self
[src]
impl<'a, T> From<&'a [T]> for &'a OutSlice<T>
[src]
impl<'a, T> From<&'a mut [MaybeUninit<T>]> for &'a mut OutSlice<T>
[src]
fn from(value: &'a mut [MaybeUninit<T>]) -> Self
[src]
impl<'a, T> From<&'a mut [T]> for &'a mut OutSlice<T>
[src]
impl<T, R: SliceIndex<[MaybeUninit<T>], Output = [MaybeUninit<T>]>> Index<R> for OutSlice<T>
[src]
impl<T, R: SliceIndex<[MaybeUninit<T>], Output = [MaybeUninit<T>]>> IndexMut<R> for OutSlice<T>
[src]
impl<'a, T> IntoIterator for &'a mut OutSlice<T>
[src]
Auto Trait Implementations
impl<T> Send for OutSlice<T> where
T: Send,
T: Send,
impl<T> !Sized for OutSlice<T>
impl<T> Sync for OutSlice<T> where
T: Sync,
T: Sync,
impl<T> Unpin for OutSlice<T> where
T: Unpin,
T: Unpin,