Sha256: f7c219b4ed309befffd0fc73a6080e47b8c5e079f0883f38f162c8b3ce5555e5

Contents?: true

Size: 1.4 KB

Versions: 11

Compression:

Stored size: 1.4 KB

Contents

//! Synchronization primitives for Wasmtime.
//!
//! This is a small set of primitives split between std and no_std with "dummy"
//! implementation on no_std. The no_std implementations live in
//! `sync_nostd.rs`.

use once_cell::sync::OnceCell;
use std::ops::{Deref, DerefMut};

/// This type is intended to mirror, and one day be implemented by, the
/// `std::sync::OnceLock` type. At this time
/// `std::sync::OnceLock::get_or_try_init` is not stable so for now this is
/// implemented with the `once_cell` crate instead.
pub struct OnceLock<T>(OnceCell<T>);

impl<T> OnceLock<T> {
    #[inline]
    pub const fn new() -> OnceLock<T> {
        OnceLock(OnceCell::new())
    }

    #[inline]
    pub fn get_or_init(&self, f: impl FnOnce() -> T) -> &T {
        self.0.get_or_init(f)
    }

    #[inline]
    pub fn get_or_try_init<E>(&self, f: impl FnOnce() -> Result<T, E>) -> Result<&T, E> {
        self.0.get_or_try_init(f)
    }
}

/// Small wrapper around `std::sync::RwLock` which undoes poisoning.
#[derive(Debug, Default)]
pub struct RwLock<T>(std::sync::RwLock<T>);

impl<T> RwLock<T> {
    #[inline]
    pub const fn new(val: T) -> RwLock<T> {
        RwLock(std::sync::RwLock::new(val))
    }

    #[inline]
    pub fn read(&self) -> impl Deref<Target = T> + '_ {
        self.0.read().unwrap()
    }

    #[inline]
    pub fn write(&self) -> impl DerefMut<Target = T> + '_ {
        self.0.write().unwrap()
    }
}

Version data entries

11 entries across 11 versions & 1 rubygems

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