Sha256: ce6452bcb3162cf687881d239f198556ae2b3e64d5e1c026e19dddfabe2a1bfb

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 KB

Contents

use core::cell::UnsafeCell;

/// A wrapper around `UnsafeCell` that implements `Send` and `Sync` for types
/// that are themselves `Send` and `Sync`.
pub struct SendSyncUnsafeCell<T>(UnsafeCell<T>);

// Safety: `T` is `Send` and users guarantee that any pointers derived from the
// inner `UnsafeCell` are used in a way that it is safe to implement `Send` and
// `Sync` for `SendSyncUnsafeCell<T>`.
unsafe impl<T: Send> Send for SendSyncUnsafeCell<T> {}
unsafe impl<T: Sync> Sync for SendSyncUnsafeCell<T> {}

impl<T> SendSyncUnsafeCell<T>
where
    T: Send + Sync,
{
    /// Create a new `SendUnsafeCell` with the given value.
    pub fn new(inner: T) -> Self {
        Self(UnsafeCell::new(inner))
    }

    /// Get an unsafe pointer to the inner value.
    ///
    /// # Safety
    ///
    /// In addition to the safety invariants of `UnsafeCell::get` that must be
    /// upheld, this pointer may only be accessed in a way that it is
    /// dynamically safe to send the underlying value between threads and share
    /// `&T` references between threads.
    pub unsafe fn get(&self) -> *mut T {
        self.0.get()
    }

    /// Get a mutable reference to the inner value.
    pub fn get_mut(&mut self) -> &mut T {
        self.0.get_mut()
    }
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
wasmtime-30.0.2 ./ext/cargo-vendor/wasmtime-30.0.2/src/runtime/vm/send_sync_unsafe_cell.rs
wasmtime-29.0.0 ./ext/cargo-vendor/wasmtime-29.0.0/src/runtime/vm/send_sync_unsafe_cell.rs
wasmtime-28.0.0 ./ext/cargo-vendor/wasmtime-28.0.0/src/runtime/vm/send_sync_unsafe_cell.rs
wasmtime-27.0.0 ./ext/cargo-vendor/wasmtime-27.0.0/src/runtime/vm/send_sync_unsafe_cell.rs