logo
  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
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
pub use std::cell::Cell;

use core::cmp::Ordering;
use core::fmt::{self, Debug};

/// A mutable Wasm-memory location.
#[repr(transparent)]
pub struct WasmCell<'a, T: ?Sized> {
    inner: &'a Cell<T>,
}

unsafe impl<T: ?Sized> Send for WasmCell<'_, T> where T: Send {}

unsafe impl<T: ?Sized> Sync for WasmCell<'_, T> {}

impl<'a, T: Copy> Clone for WasmCell<'a, T> {
    #[inline]
    fn clone(&self) -> WasmCell<'a, T> {
        WasmCell { inner: self.inner }
    }
}

impl<T: PartialEq + Copy> PartialEq for WasmCell<'_, T> {
    #[inline]
    fn eq(&self, other: &WasmCell<T>) -> bool {
        self.inner.eq(&other.inner)
    }
}

impl<T: Eq + Copy> Eq for WasmCell<'_, T> {}

impl<T: PartialOrd + Copy> PartialOrd for WasmCell<'_, T> {
    #[inline]
    fn partial_cmp(&self, other: &WasmCell<T>) -> Option<Ordering> {
        self.inner.partial_cmp(&other.inner)
    }

    #[inline]
    fn lt(&self, other: &WasmCell<T>) -> bool {
        self.inner < other.inner
    }

    #[inline]
    fn le(&self, other: &WasmCell<T>) -> bool {
        self.inner <= other.inner
    }

    #[inline]
    fn gt(&self, other: &WasmCell<T>) -> bool {
        self.inner > other.inner
    }

    #[inline]
    fn ge(&self, other: &WasmCell<T>) -> bool {
        self.inner >= other.inner
    }
}

impl<T: Ord + Copy> Ord for WasmCell<'_, T> {
    #[inline]
    fn cmp(&self, other: &WasmCell<T>) -> Ordering {
        self.inner.cmp(&other.inner)
    }
}

impl<'a, T> WasmCell<'a, T> {
    /// Creates a new `WasmCell` containing the given value.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::cell::Cell;
    /// use wasmer::WasmCell;
    ///
    /// let cell = Cell::new(5);
    /// let wasm_cell = WasmCell::new(&cell);
    /// ```
    #[inline]
    pub const fn new(cell: &'a Cell<T>) -> WasmCell<'a, T> {
        WasmCell { inner: cell }
    }
}

impl<'a, T: Copy> WasmCell<'a, T> {
    /// Returns a copy of the contained value.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::cell::Cell;
    /// use wasmer::WasmCell;
    ///
    /// let cell = Cell::new(5);
    /// let wasm_cell = WasmCell::new(&cell);
    /// let five = wasm_cell.get();
    /// ```
    #[inline]
    pub fn get(&self) -> T {
        self.inner.get()
    }

    /// Get an unsafe mutable pointer to the inner item
    /// in the Cell.
    ///
    /// # Safety
    ///
    /// This method is highly discouraged to use. We have it for
    /// compatibility reasons with Emscripten.
    /// It is unsafe because changing an item inline will change
    /// the underlying memory.
    ///
    /// It's highly encouraged to use the `set` method instead.
    #[deprecated(
        since = "2.0.0",
        note = "Please use the memory-safe set method instead"
    )]
    #[doc(hidden)]
    pub unsafe fn get_mut(&self) -> &'a mut T {
        &mut *self.inner.as_ptr()
    }
}

impl<T: Debug + Copy> Debug for WasmCell<'_, T> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "WasmCell({:?})", self.inner.get())
    }
}

impl<T: Sized> WasmCell<'_, T> {
    /// Sets the contained value.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::cell::Cell;
    /// use wasmer::WasmCell;
    ///
    /// let cell = Cell::new(5);
    /// let wasm_cell = WasmCell::new(&cell);
    /// wasm_cell.set(10);
    /// assert_eq!(cell.get(), 10);
    /// ```
    #[inline]
    pub fn set(&self, val: T) {
        self.inner.set(val);
    }
}