Skip to content

Commit

Permalink
add Send restriction to resource table
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Jun 17, 2024
1 parent 6af0a54 commit c677675
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions crates/wasm-bridge/src/js/component/resource_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ use super::Resource;

// TODO: unify with wasi's resource table?
#[derive(Default, Debug)]
pub struct ResourceTable(Slab<Box<dyn Any>>);
pub struct ResourceTable(Slab<Box<dyn Any + Send>>);

impl ResourceTable {
pub fn new() -> Self {
Self::default()
}

pub fn push<R: Any>(&mut self, value: R) -> Result<Resource<R>, ResourceTableError> {
pub fn push<R: Any + Send>(&mut self, value: R) -> Result<Resource<R>, ResourceTableError> {
let index = self.0.insert(Box::new(value));
Ok(Resource::new_own(index as u32))
}

pub fn get<R: Any>(&self, resource: &Resource<R>) -> Result<&R, ResourceTableError> {
pub fn get<R: Any + Send>(&self, resource: &Resource<R>) -> Result<&R, ResourceTableError> {
self.0
.get(resource.rep() as usize)
.ok_or(ResourceTableError::NotPresent)?
.downcast_ref()
.ok_or(ResourceTableError::WrongType)
}

pub fn get_mut<R: Any>(
pub fn get_mut<R: Any + Send>(
&mut self,
resource: &Resource<R>,
) -> Result<&mut R, ResourceTableError> {
Expand All @@ -37,7 +37,10 @@ impl ResourceTable {
.ok_or(ResourceTableError::WrongType)
}

pub fn delete<R: Any>(&mut self, resource: Resource<R>) -> Result<R, ResourceTableError> {
pub fn delete<R: Any + Send>(
&mut self,
resource: Resource<R>,
) -> Result<R, ResourceTableError> {
Ok(*(self
.0
.try_remove(resource.rep() as usize)
Expand Down

0 comments on commit c677675

Please sign in to comment.