1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//!
//! Sendable NewType for automatic Send marker tagging of JS primitives.
//!

///
/// Senable wrapper for JS primitives.
///
/// Wrapping any JS primitive (JsValue, JsString, JsArray, JsObject, etc.) in
/// Sendable<T> wraps the value with the Send marker, making it transportable
/// across "thread boundaries". In reality, this allows JS primitives to be
/// used safely within a single-threaded WASM async environment (browser).
///
#[derive(Clone)]
pub struct Sendable<T>(pub T)
where
    T: Clone;
unsafe impl<T> Send for Sendable<T> where T: Clone {}

impl<T> std::ops::Deref for Sendable<T>
where
    T: Clone,
{
    type Target = T;
    fn deref(&self) -> &T {
        &self.0
    }
}

impl<T> AsRef<T> for Sendable<T>
where
    T: Clone,
{
    fn as_ref(&self) -> &T {
        &self.0
    }
}

impl<T> AsMut<T> for Sendable<T>
where
    T: Clone,
{
    fn as_mut(&mut self) -> &mut T {
        &mut self.0
    }
}

impl<T> From<T> for Sendable<T>
where
    T: Clone,
{
    fn from(t: T) -> Self {
        Sendable(t)
    }
}