Sha256: 722e82f3c10343f29f978d3b2ec701367e833f7baaaba8972962f4e7a602521d

Contents?: true

Size: 1.47 KB

Versions: 11

Compression:

Stored size: 1.47 KB

Contents

use crate::ValRaw;
use core::mem::{self, MaybeUninit};
use core::slice;

fn assert_raw_slice_compat<T>() {
    assert!(mem::size_of::<T>() % mem::size_of::<ValRaw>() == 0);
    assert!(mem::align_of::<T>() == mem::align_of::<ValRaw>());
}

/// Converts a `<T as ComponentType>::Lower` representation to a slice of
/// `ValRaw`.
pub unsafe fn storage_as_slice<T>(storage: &T) -> &[ValRaw] {
    assert_raw_slice_compat::<T>();

    slice::from_raw_parts(
        (storage as *const T).cast(),
        mem::size_of_val(storage) / mem::size_of::<ValRaw>(),
    )
}

/// Same as `storage_as_slice`, but mutable.
pub unsafe fn storage_as_slice_mut<T>(storage: &mut T) -> &mut [ValRaw] {
    assert_raw_slice_compat::<T>();

    slice::from_raw_parts_mut(
        (storage as *mut T).cast(),
        mem::size_of_val(storage) / mem::size_of::<ValRaw>(),
    )
}

/// Same as `storage_as_slice`, but in reverse and mutable.
pub unsafe fn slice_to_storage_mut<T>(slice: &mut [MaybeUninit<ValRaw>]) -> &mut MaybeUninit<T> {
    assert_raw_slice_compat::<T>();

    // This is an actual runtime assertion which if performance calls for we may
    // need to relax to a debug assertion. This notably tries to ensure that we
    // stay within the bounds of the number of actual values given rather than
    // reading past the end of an array. This shouldn't actually trip unless
    // there's a bug in Wasmtime though.
    assert!(mem::size_of_val(slice) >= mem::size_of::<T>());

    &mut *slice.as_mut_ptr().cast()
}

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/wasmtime-29.0.0/src/runtime/component/storage.rs
wasmtime-28.0.0 ./ext/cargo-vendor/wasmtime-28.0.0/src/runtime/component/storage.rs
wasmtime-27.0.0 ./ext/cargo-vendor/wasmtime-27.0.0/src/runtime/component/storage.rs
wasmtime-26.0.0 ./ext/cargo-vendor/wasmtime-26.0.0/src/runtime/component/storage.rs
wasmtime-25.0.2 ./ext/cargo-vendor/wasmtime-25.0.2/src/runtime/component/storage.rs
wasmtime-25.0.1 ./ext/cargo-vendor/wasmtime-25.0.1/src/runtime/component/storage.rs
wasmtime-25.0.0 ./ext/cargo-vendor/wasmtime-25.0.0/src/runtime/component/storage.rs
wasmtime-24.0.0 ./ext/cargo-vendor/wasmtime-24.0.0/src/runtime/component/storage.rs
wasmtime-23.0.2 ./ext/cargo-vendor/wasmtime-23.0.2/src/runtime/component/storage.rs
wasmtime-22.0.0 ./ext/cargo-vendor/wasmtime-22.0.0/src/runtime/component/storage.rs
wasmtime-21.0.1 ./ext/cargo-vendor/wasmtime-21.0.1/src/runtime/component/storage.rs