Skip to main content

AssumeInit

Trait AssumeInit 

Source
pub trait AssumeInit {
    type Output;

    // Required method
    unsafe fn assume_init(self) -> Self::Output;
}
Expand description

Trait for converting collections of uninitialized (MaybeUninit<T>) values to collections of corresponding initializes values (T).

§Example

use std::mem::MaybeUninit;
use rten_tensor::AssumeInit;

fn scale_values<'a>(dst: &'a mut [MaybeUninit<f32>], src: &[f32], scale: f32) -> &'a mut [f32] {
  for (y, x) in dst.into_iter().zip(src) {
    y.write(x * scale);
  }
  // Safety: All elements have been initialized.
  unsafe { dst.assume_init() }
}

let src = [1., 2., 3.];
let mut dst = [MaybeUninit::uninit(); 3];
let scaled = scale_values(&mut dst, &src, 2.);
assert_eq!(scaled, [2., 4., 6.]);

Required Associated Types§

Source

type Output

The type of the initialized storage.

Required Methods§

Source

unsafe fn assume_init(self) -> Self::Output

Cast self to a collection of initialized values.

§Safety

The caller must guarantee that all elements have been initialized.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<'a, T> AssumeInit for &'a [MaybeUninit<T>]

Source§

type Output = &'a [T]

Source§

unsafe fn assume_init(self) -> Self::Output

Source§

impl<'a, T> AssumeInit for &'a mut [MaybeUninit<T>]

Source§

type Output = &'a mut [T]

Source§

unsafe fn assume_init(self) -> Self::Output

Source§

impl<T> AssumeInit for Vec<MaybeUninit<T>>

Source§

type Output = Vec<T>

Source§

unsafe fn assume_init(self) -> Self::Output

Implementors§