Sha256: e2c1b86ea97ca5c61d562182890cbe24c7eaa37ff8945c17fcfa665b767da1b0

Contents?: true

Size: 1.38 KB

Versions: 8

Compression:

Stored size: 1.38 KB

Contents

#![allow(dead_code)]
use core::{mem::MaybeUninit, ptr};

/// Polyfill for `maybe_uninit_slice` feature's
/// `MaybeUninit::slice_assume_init_mut`. Every element of `slice` must have
/// been initialized.
#[inline(always)]
pub unsafe fn slice_assume_init_mut<T>(slice: &mut [MaybeUninit<T>]) -> &mut [T] {
    // SAFETY: `MaybeUninit<T>` is guaranteed to be layout-compatible with `T`.
    &mut *(slice as *mut [MaybeUninit<T>] as *mut [T])
}

#[inline]
pub fn uninit_slice_fill_zero(slice: &mut [MaybeUninit<u8>]) -> &mut [u8] {
    unsafe { ptr::write_bytes(slice.as_mut_ptr(), 0, slice.len()) };
    unsafe { slice_assume_init_mut(slice) }
}

#[inline(always)]
pub fn slice_as_uninit<T>(slice: &[T]) -> &[MaybeUninit<T>] {
    // SAFETY: `MaybeUninit<T>` is guaranteed to be layout-compatible with `T`.
    // There is no risk of writing a `MaybeUninit<T>` into the result since
    // the result isn't mutable.
    unsafe { &*(slice as *const [T] as *const [MaybeUninit<T>]) }
}

/// View an mutable initialized array as potentially-uninitialized.
///
/// This is unsafe because it allows assigning uninitialized values into
/// `slice`, which would be undefined behavior.
#[inline(always)]
pub unsafe fn slice_as_uninit_mut<T>(slice: &mut [T]) -> &mut [MaybeUninit<T>] {
    // SAFETY: `MaybeUninit<T>` is guaranteed to be layout-compatible with `T`.
    &mut *(slice as *mut [T] as *mut [MaybeUninit<T>])
}

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/getrandom-0.2.15/src/util.rs
wasmtime-28.0.0 ./ext/cargo-vendor/getrandom-0.2.15/src/util.rs
wasmtime-27.0.0 ./ext/cargo-vendor/getrandom-0.2.15/src/util.rs
wasmtime-26.0.0 ./ext/cargo-vendor/getrandom-0.2.15/src/util.rs
wasmtime-25.0.2 ./ext/cargo-vendor/getrandom-0.2.15/src/util.rs
wasmtime-25.0.1 ./ext/cargo-vendor/getrandom-0.2.15/src/util.rs
wasmtime-25.0.0 ./ext/cargo-vendor/getrandom-0.2.15/src/util.rs
wasmtime-24.0.0 ./ext/cargo-vendor/getrandom-0.2.15/src/util.rs