Skip to content

Commit 5ede1ac

Browse files
committed
Improved API by using references
1 parent dc84bd4 commit 5ede1ac

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

lib/api/src/cell.rs

+20-14
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ use core::ptr;
4242
/// See the [module-level documentation](self) for more.
4343
#[derive(Clone)]
4444
#[repr(transparent)]
45-
pub struct WasmCell<T: ?Sized> {
46-
inner: *const Cell<T>,
45+
pub struct WasmCell<'a, T: ?Sized> {
46+
inner: &'a Cell<T>,
4747
}
4848

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

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

5353
// impl<T: Copy> Clone for WasmCell<T> {
5454
// #[inline]
@@ -67,16 +67,16 @@ unsafe impl<T: ?Sized> Sync for WasmCell<T> {}
6767
// }
6868
// }
6969

70-
impl<T: PartialEq + Copy> PartialEq for WasmCell<T> {
70+
impl<T: PartialEq + Copy> PartialEq for WasmCell<'_, T> {
7171
#[inline]
7272
fn eq(&self, other: &WasmCell<T>) -> bool {
7373
true
7474
}
7575
}
7676

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

79-
impl<T: PartialOrd + Copy> PartialOrd for WasmCell<T> {
79+
impl<T: PartialOrd + Copy> PartialOrd for WasmCell<'_, T> {
8080
#[inline]
8181
fn partial_cmp(&self, other: &WasmCell<T>) -> Option<Ordering> {
8282
self.inner.partial_cmp(&other.inner)
@@ -103,7 +103,7 @@ impl<T: PartialOrd + Copy> PartialOrd for WasmCell<T> {
103103
}
104104
}
105105

106-
impl<T: Ord + Copy> Ord for WasmCell<T> {
106+
impl<T: Ord + Copy> Ord for WasmCell<'_, T> {
107107
#[inline]
108108
fn cmp(&self, other: &WasmCell<T>) -> Ordering {
109109
self.get().cmp(&other.get())
@@ -117,7 +117,7 @@ impl<T: Ord + Copy> Ord for WasmCell<T> {
117117
// }
118118
// }
119119

120-
impl<T> WasmCell<T> {
120+
impl<'a, T> WasmCell<'a, T> {
121121
/// Creates a new `WasmCell` containing the given value.
122122
///
123123
/// # Examples
@@ -128,7 +128,7 @@ impl<T> WasmCell<T> {
128128
/// let c = WasmCell::new(5);
129129
/// ```
130130
#[inline]
131-
pub const fn new(cell: *const Cell<T>) -> WasmCell<T> {
131+
pub const fn new(cell: &'a Cell<T>) -> WasmCell<'a, T> {
132132
WasmCell {
133133
inner: cell,
134134
}
@@ -202,7 +202,7 @@ impl<T> WasmCell<T> {
202202
// }
203203
}
204204

205-
impl<T: Copy> WasmCell<T> {
205+
impl<T: Copy> WasmCell<'_, T> {
206206
/// Returns a copy of the contained value.
207207
///
208208
/// # Examples
@@ -216,11 +216,17 @@ impl<T: Copy> WasmCell<T> {
216216
/// ```
217217
#[inline]
218218
pub fn get(&self) -> T {
219-
unsafe { (*self.inner).get() }
219+
self.inner.get()
220+
}
221+
222+
/// Get an unsafe mutable pointer to the inner item
223+
/// in the Cell.
224+
pub unsafe fn get_mut(&self) -> &mut T {
225+
&mut *self.inner.as_ptr()
220226
}
221227
}
222228

223-
impl<T: Sized> WasmCell<T> {
229+
impl<T: Sized> WasmCell<'_, T> {
224230
/// Sets the contained value.
225231
///
226232
/// # Examples
@@ -234,6 +240,6 @@ impl<T: Sized> WasmCell<T> {
234240
/// ```
235241
#[inline]
236242
pub fn set(&self, val: T) {
237-
unsafe { (*self.inner).set(val) };
243+
self.inner.set(val);
238244
}
239245
}

lib/api/src/ptr.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl<T: Copy + ValueType> WasmPtr<T, Item> {
115115
memory.view::<u8>().as_ptr().add(self.offset as usize) as usize,
116116
mem::align_of::<T>(),
117117
) as *const Cell<T>;
118-
Some(WasmCell::new(cell_ptr))
118+
Some(WasmCell::new(&*cell_ptr))
119119
}
120120
}
121121

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

0 commit comments

Comments
 (0)