Implement RFC 11: Redesigning Wasmtime's APIs (#2897)
Implement Wasmtime's new API as designed by RFC 11. This is quite a large commit which has had lots of discussion externally, so for more information it's best to read the RFC thread and the PR thread.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
use crate::{Error, ErrorExt};
|
||||
use std::any::Any;
|
||||
use std::cell::{Ref, RefCell, RefMut};
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// The `Table` type is designed to map u32 handles to resources. The table is now part of the
|
||||
@@ -11,7 +10,7 @@ use std::collections::HashMap;
|
||||
/// The `Table` type is intended to model how the Interface Types concept of Resources is shaping
|
||||
/// up. Right now it is just an approximation.
|
||||
pub struct Table {
|
||||
map: HashMap<u32, RefCell<Box<dyn Any>>>,
|
||||
map: HashMap<u32, Box<dyn Any + Send + Sync>>,
|
||||
next_key: u32,
|
||||
}
|
||||
|
||||
@@ -25,12 +24,12 @@ impl Table {
|
||||
}
|
||||
|
||||
/// Insert a resource at a certain index.
|
||||
pub fn insert_at(&mut self, key: u32, a: Box<dyn Any>) {
|
||||
self.map.insert(key, RefCell::new(a));
|
||||
pub fn insert_at(&mut self, key: u32, a: Box<dyn Any + Send + Sync>) {
|
||||
self.map.insert(key, a);
|
||||
}
|
||||
|
||||
/// Insert a resource at the next available index.
|
||||
pub fn push(&mut self, a: Box<dyn Any>) -> Result<u32, Error> {
|
||||
pub fn push(&mut self, a: Box<dyn Any + Send + Sync>) -> Result<u32, Error> {
|
||||
// NOTE: The performance of this new key calculation could be very bad once keys wrap
|
||||
// around.
|
||||
if self.map.len() == u32::MAX as usize {
|
||||
@@ -42,7 +41,7 @@ impl Table {
|
||||
if self.map.contains_key(&key) {
|
||||
continue;
|
||||
}
|
||||
self.map.insert(key, RefCell::new(a));
|
||||
self.map.insert(key, a);
|
||||
return Ok(key);
|
||||
}
|
||||
}
|
||||
@@ -55,12 +54,8 @@ impl Table {
|
||||
/// Check if the resource at a given index can be downcast to a given type.
|
||||
/// Note: this will always fail if the resource is already borrowed.
|
||||
pub fn is<T: Any + Sized>(&self, key: u32) -> bool {
|
||||
if let Some(refcell) = self.map.get(&key) {
|
||||
if let Ok(refmut) = refcell.try_borrow_mut() {
|
||||
refmut.is::<T>()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
if let Some(r) = self.map.get(&key) {
|
||||
r.is::<T>()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
@@ -69,17 +64,10 @@ impl Table {
|
||||
/// Get an immutable reference to a resource of a given type at a given index. Multiple
|
||||
/// immutable references can be borrowed at any given time. Borrow failure
|
||||
/// results in a trapping error.
|
||||
pub fn get<T: Any + Sized>(&self, key: u32) -> Result<Ref<T>, Error> {
|
||||
if let Some(refcell) = self.map.get(&key) {
|
||||
if let Ok(r) = refcell.try_borrow() {
|
||||
if r.is::<T>() {
|
||||
Ok(Ref::map(r, |r| r.downcast_ref::<T>().unwrap()))
|
||||
} else {
|
||||
Err(Error::badf().context("element is a different type"))
|
||||
}
|
||||
} else {
|
||||
Err(Error::trap("table get of mutably borrowed element"))
|
||||
}
|
||||
pub fn get<T: Any + Sized>(&self, key: u32) -> Result<&T, Error> {
|
||||
if let Some(r) = self.map.get(&key) {
|
||||
r.downcast_ref::<T>()
|
||||
.ok_or_else(|| Error::badf().context("element is a different type"))
|
||||
} else {
|
||||
Err(Error::badf().context("key not in table"))
|
||||
}
|
||||
@@ -87,17 +75,10 @@ impl Table {
|
||||
|
||||
/// Get a mutable reference to a resource of a given type at a given index. Only one mutable
|
||||
/// reference can be borrowed at any given time. Borrow failure results in a trapping error.
|
||||
pub fn get_mut<T: Any + Sized>(&self, key: u32) -> Result<RefMut<T>, Error> {
|
||||
if let Some(refcell) = self.map.get(&key) {
|
||||
if let Ok(r) = refcell.try_borrow_mut() {
|
||||
if r.is::<T>() {
|
||||
Ok(RefMut::map(r, |r| r.downcast_mut::<T>().unwrap()))
|
||||
} else {
|
||||
Err(Error::badf().context("element is a different type"))
|
||||
}
|
||||
} else {
|
||||
Err(Error::trap("table get_mut of borrowed element"))
|
||||
}
|
||||
pub fn get_mut<T: Any + Sized>(&mut self, key: u32) -> Result<&mut T, Error> {
|
||||
if let Some(r) = self.map.get_mut(&key) {
|
||||
r.downcast_mut::<T>()
|
||||
.ok_or_else(|| Error::badf().context("element is a different type"))
|
||||
} else {
|
||||
Err(Error::badf().context("key not in table"))
|
||||
}
|
||||
@@ -105,7 +86,7 @@ impl Table {
|
||||
|
||||
/// Remove a resource at a given index from the table. Returns the resource
|
||||
/// if it was present.
|
||||
pub fn delete(&mut self, key: u32) -> Option<Box<dyn Any>> {
|
||||
self.map.remove(&key).map(|rc| RefCell::into_inner(rc))
|
||||
pub fn delete(&mut self, key: u32) -> Option<Box<dyn Any + Send + Sync>> {
|
||||
self.map.remove(&key)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user