Skip to content

Commit

Permalink
Improved API by using references
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Jun 24, 2021
1 parent dc84bd4 commit 5ede1ac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
34 changes: 20 additions & 14 deletions lib/api/src/cell.rs
Expand Up @@ -42,13 +42,13 @@ use core::ptr;
/// See the [module-level documentation](self) for more.
#[derive(Clone)]
#[repr(transparent)]
pub struct WasmCell<T: ?Sized> {
inner: *const Cell<T>,
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> Send for WasmCell<'_, T> where T: Send {}

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

// impl<T: Copy> Clone for WasmCell<T> {
// #[inline]
Expand All @@ -67,16 +67,16 @@ unsafe impl<T: ?Sized> Sync for WasmCell<T> {}
// }
// }

impl<T: PartialEq + Copy> PartialEq for WasmCell<T> {
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: Eq + Copy> Eq for WasmCell<'_, T> {}

impl<T: PartialOrd + Copy> PartialOrd 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)
Expand All @@ -103,7 +103,7 @@ impl<T: PartialOrd + Copy> PartialOrd for WasmCell<T> {
}
}

impl<T: Ord + Copy> Ord for WasmCell<T> {
impl<T: Ord + Copy> Ord for WasmCell<'_, T> {
#[inline]
fn cmp(&self, other: &WasmCell<T>) -> Ordering {
self.get().cmp(&other.get())
Expand All @@ -117,7 +117,7 @@ impl<T: Ord + Copy> Ord for WasmCell<T> {
// }
// }

impl<T> WasmCell<T> {
impl<'a, T> WasmCell<'a, T> {
/// Creates a new `WasmCell` containing the given value.
///
/// # Examples
Expand All @@ -128,7 +128,7 @@ impl<T> WasmCell<T> {
/// let c = WasmCell::new(5);
/// ```
#[inline]
pub const fn new(cell: *const Cell<T>) -> WasmCell<T> {
pub const fn new(cell: &'a Cell<T>) -> WasmCell<'a, T> {
WasmCell {
inner: cell,
}
Expand Down Expand Up @@ -202,7 +202,7 @@ impl<T> WasmCell<T> {
// }
}

impl<T: Copy> WasmCell<T> {
impl<T: Copy> WasmCell<'_, T> {
/// Returns a copy of the contained value.
///
/// # Examples
Expand All @@ -216,11 +216,17 @@ impl<T: Copy> WasmCell<T> {
/// ```
#[inline]
pub fn get(&self) -> T {
unsafe { (*self.inner).get() }
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> {
impl<T: Sized> WasmCell<'_, T> {
/// Sets the contained value.
///
/// # Examples
Expand All @@ -234,6 +240,6 @@ impl<T: Sized> WasmCell<T> {
/// ```
#[inline]
pub fn set(&self, val: T) {
unsafe { (*self.inner).set(val) };
self.inner.set(val);
}
}
10 changes: 5 additions & 5 deletions lib/api/src/ptr.rs
Expand Up @@ -115,7 +115,7 @@ impl<T: Copy + ValueType> WasmPtr<T, Item> {
memory.view::<u8>().as_ptr().add(self.offset as usize) as usize,
mem::align_of::<T>(),
) as *const Cell<T>;
Some(WasmCell::new(cell_ptr))
Some(WasmCell::new(&*cell_ptr))
}
}

Expand Down Expand Up @@ -152,7 +152,7 @@ impl<T: Copy + ValueType> WasmPtr<T, Array> {
/// If you're unsure what that means, it likely does not apply to you.
/// This invariant will be enforced in the future.
#[inline]
pub fn deref(self, memory: &Memory, index: u32, length: u32) -> Option<&[Cell<T>]> {
pub fn deref(self, memory: &Memory, index: u32, length: u32) -> Option<Box<[WasmCell<T>]>> {
// gets the size of the item in the array with padding added such that
// for any index, we will always result an aligned memory access
let item_size = mem::size_of::<T>();
Expand All @@ -170,9 +170,9 @@ impl<T: Copy + ValueType> WasmPtr<T, Array> {
let cell_ptr = align_pointer(
memory.view::<u8>().as_ptr().add(self.offset as usize) as usize,
mem::align_of::<T>(),
) as *const Cell<T>;
let cell_ptrs = &std::slice::from_raw_parts(cell_ptr, slice_full_len)
[index as usize..slice_full_len];
) as *const WasmCell<T>;
let cell_ptrs = std::slice::from_raw_parts(cell_ptr, slice_full_len)
[index as usize..slice_full_len].to_owned().into_boxed_slice();
Some(cell_ptrs)
}
}
Expand Down

0 comments on commit 5ede1ac

Please sign in to comment.