-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2442: Improved WasmPtr, added WasmCell r=syrusakbary a=syrusakbary # Description This PR improves a bit our WasmPtr API by making it a bit more safer and reliable. ## Breaking changes * `WasmPtr`.`deref` will now return `WasmCell<'a, T>` instead of `&'a Cell<T>` * `WasmPtr`.`deref_mut` is no needed any longer Co-authored-by: Syrus Akbary <[email protected]>
- Loading branch information
Showing
11 changed files
with
297 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
pub use std::cell::Cell; | ||
|
||
use core::cmp::Ordering; | ||
use core::fmt::{self, Debug}; | ||
use std::fmt::Pointer; | ||
|
||
/// A mutable Wasm-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. | ||
#[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 wasmer::WasmCell; | ||
/// | ||
/// let c = WasmCell::new(5); | ||
/// ``` | ||
#[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 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. | ||
/// | ||
/// # 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> Debug for WasmCell<'_, T> { | ||
#[inline] | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.inner.fmt(f) | ||
} | ||
} | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.