Struct possibly_uninit::slice::OutSlice[][src]

pub struct OutSlice<T>(_);

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]

Accesses the value at given index.

Note that this is useless unless you know that the value is initialized. Declare so by unsafely calling assume_init_ref.

pub fn at_mut<I: SliceIndex<[MaybeUninit<T>], Output = MaybeUninit<T>>>(
    &mut self,
    index: I
) -> Out<'_, T>
[src]

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>

Notable traits for IterMut<'a, T>

impl<'a, T> Iterator for IterMut<'a, T> type Item = Out<'a, 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]

Splits the slice at given index into two parts.

Panics

Panics if mid is out of bounds.

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]

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]

Uses the iterator to initialize the slice

pub fn copy_from_slice(&mut self, slice: &[T]) -> &mut [T] where
    T: Copy
[src]

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]

impl<T> BorrowUninitSlice<T> for OutSlice<T>[src]

impl<'a, T> From<&'a [MaybeUninit<T>]> for &'a OutSlice<T>[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]

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]

type Output = Self

The returned type after indexing.

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]

type Item = Out<'a, T>

The type of the elements being iterated over.

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?

Auto Trait Implementations

impl<T> Send for OutSlice<T> where
    T: Send

impl<T> !Sized for OutSlice<T>

impl<T> Sync for OutSlice<T> where
    T: Sync

impl<T> Unpin for OutSlice<T> where
    T: Unpin

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]