-
Notifications
You must be signed in to change notification settings - Fork 757
/
cell.rs
245 lines (221 loc) 路 5.81 KB
/
cell.rs
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
pub use std::cell::Cell;
use core::cmp::Ordering;
use core::fmt::{self, Debug, Display};
use core::mem;
use core::ops::{Deref, DerefMut};
use core::ptr;
/// A mutable memory location.
///
/// # Examples
///
/// In this example, you can see that `WasmCell<T>` enables mutation inside an
/// immutable struct. In other words, it enables "interior mutability".
///
/// ```
/// use wasmer::WasmCell;
///
/// struct SomeStruct {
/// regular_field: u8,
/// special_field: WasmCell<u8>,
/// }
///
/// let my_struct = SomeStruct {
/// regular_field: 0,
/// special_field: WasmCell::new(1),
/// };
///
/// let new_value = 100;
///
/// // ERROR: `my_struct` is immutable
/// // my_struct.regular_field = new_value;
///
/// // WORKS: although `my_struct` is immutable, `special_field` is a `WasmCell`,
/// // which can always be mutated
/// my_struct.special_field.set(new_value);
/// assert_eq!(my_struct.special_field.get(), new_value);
/// ```
///
/// See the [module-level documentation](self) for more.
#[derive(Clone)]
#[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<T: Copy> Clone for WasmCell<T> {
// #[inline]
// fn clone(&self) -> WasmCell<T> {
// WasmCell::new(self.get())
// }
// }
// impl<T: Default> Default for WasmCell<T> {
// /// Creates a `WasmCell<T>`, with the `Default` value for T.
// #[inline]
// fn default() -> WasmCell<T> {
// WasmCell::new({
// inner: Default::default()
// )
// }
// }
impl<T: PartialEq + Copy> PartialEq for WasmCell<'_, T> {
#[inline]
fn eq(&self, other: &WasmCell<T>) -> bool {
true
}
}
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.get().cmp(&other.get())
}
}
// impl<T> From<T> for WasmCell<T> {
// fn from(t: T) -> WasmCell<T> {
// // unimplemented!();
// // WasmCell::new(t)
// }
// }
impl<'a, T> WasmCell<'a, T> {
/// Creates a new `WasmCell` containing the given value.
///
/// # Examples
///
/// ```
/// use wasmer::WasmCell;
///
/// let c = WasmCell::new(5);
/// ```
#[inline]
pub const fn new(cell: &'a Cell<T>) -> WasmCell<'a, T> {
WasmCell {
inner: cell,
}
}
/// Swaps the values of two WasmCells.
/// Difference with `std::mem::swap` is that this function doesn't require `&mut` reference.
///
/// # Examples
///
/// ```
/// use wasmer::WasmCell;
///
/// let c1 = WasmCell::new(5i32);
/// let c2 = WasmCell::new(10i32);
/// c1.swap(&c2);
/// assert_eq!(10, c1.get());
/// assert_eq!(5, c2.get());
/// ```
#[inline]
pub fn swap(&self, other: &Self) {
unimplemented!();
// if ptr::eq(self, other) {
// return;
// }
// // SAFETY: This can be risky if called from separate threads, but `WasmCell`
// // is `!Sync` so this won't happen. This also won't invalidate any
// // pointers since `WasmCell` makes sure nothing else will be pointing into
// // either of these `WasmCell`s.
// unsafe {
// ptr::swap(self.value.get(), other.value.get());
// }
}
/// Replaces the contained value with `val`, and returns the old contained value.
///
/// # Examples
///
/// ```
/// use wasmer::WasmCell;
///
/// let cell = WasmCell::new(5);
/// assert_eq!(cell.get(), 5);
/// assert_eq!(cell.replace(10), 5);
/// assert_eq!(cell.get(), 10);
/// ```
pub fn replace(&self, val: T) -> T {
unimplemented!();
// SAFETY: This can cause data races if called from a separate thread,
// but `WasmCell` is `!Sync` so this won't happen.
// mem::replace(unsafe { &mut *self.value.get() }, val)
}
// /// Unwraps the value.
// ///
// /// # Examples
// ///
// /// ```
// /// use wasmer::WasmCell;
// ///
// /// let c = WasmCell::new(5);
// /// let five = c.into_inner();
// ///
// /// assert_eq!(five, 5);
// /// ```
// pub const fn into_inner(self) -> T {
// // This will get the item out of the MemoryView and into
// // Rust memory allocator
// unimplemented!()
// // self.get()
// }
}
impl<T: Copy> WasmCell<'_, T> {
/// Returns a copy of the contained value.
///
/// # Examples
///
/// ```
/// use wasmer::WasmCell;
///
/// let c = WasmCell::new(5);
///
/// let five = c.get();
/// ```
#[inline]
pub fn get(&self) -> T {
self.inner.get()
}
/// Get an unsafe mutable pointer to the inner item
/// in the Cell.
pub unsafe fn get_mut(&self) -> &mut T {
&mut *self.inner.as_ptr()
}
}
impl<T: Sized> WasmCell<'_, T> {
/// Sets the contained value.
///
/// # Examples
///
/// ```
/// use wasmer::WasmCell;
///
/// let c = WasmCell::new(5);
///
/// c.set(10);
/// ```
#[inline]
pub fn set(&self, val: T) {
self.inner.set(val);
}
}