//! 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(OnceCell); impl OnceLock { #[inline] pub const fn new() -> OnceLock { 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(&self, f: impl FnOnce() -> Result) -> Result<&T, E> { self.0.get_or_try_init(f) } } impl Default for OnceLock { fn default() -> OnceLock { OnceLock::new() } } /// Small wrapper around `std::sync::RwLock` which undoes poisoning. #[derive(Debug, Default)] pub struct RwLock(std::sync::RwLock); impl RwLock { #[inline] pub const fn new(val: T) -> RwLock { RwLock(std::sync::RwLock::new(val)) } #[inline] pub fn read(&self) -> impl Deref + '_ { self.0.read().unwrap() } #[inline] pub fn write(&self) -> impl DerefMut + '_ { self.0.write().unwrap() } }