wasmtime/runtime/vm/
send_sync_unsafe_cell.rs

1use core::cell::UnsafeCell;
2
3/// A wrapper around `UnsafeCell` that implements `Send` and `Sync` for types
4/// that are themselves `Send` and `Sync`.
5pub struct SendSyncUnsafeCell<T>(UnsafeCell<T>);
6
7// Safety: `T` is `Send` and users guarantee that any pointers derived from the
8// inner `UnsafeCell` are used in a way that it is safe to implement `Send` and
9// `Sync` for `SendSyncUnsafeCell<T>`.
10unsafe impl<T: Send> Send for SendSyncUnsafeCell<T> {}
11unsafe impl<T: Sync> Sync for SendSyncUnsafeCell<T> {}
12
13impl<T> SendSyncUnsafeCell<T>
14where
15    T: Send + Sync,
16{
17    /// Create a new `SendUnsafeCell` with the given value.
18    pub fn new(inner: T) -> Self {
19        Self(UnsafeCell::new(inner))
20    }
21
22    /// Get an unsafe pointer to the inner value.
23    ///
24    /// # Safety
25    ///
26    /// In addition to the safety invariants of `UnsafeCell::get` that must be
27    /// upheld, this pointer may only be accessed in a way that it is
28    /// dynamically safe to send the underlying value between threads and share
29    /// `&T` references between threads.
30    pub unsafe fn get(&self) -> *mut T {
31        self.0.get()
32    }
33
34    /// Get a mutable reference to the inner value.
35    pub fn get_mut(&mut self) -> &mut T {
36        self.0.get_mut()
37    }
38}