use std::any::Any;
use wasmtime_slab::{Id, Slab};
#[derive(Default)]
pub struct ExternRefHostDataTable {
slab: Slab<Box<dyn Any + Send + Sync>>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct ExternRefHostDataId(Id);
fn deref_box<T: ?Sized>(b: &Box<T>) -> &T {
&**b
}
fn deref_box_mut<T: ?Sized>(b: &mut Box<T>) -> &mut T {
&mut **b
}
impl ExternRefHostDataTable {
pub fn alloc(&mut self, value: Box<dyn Any + Send + Sync>) -> ExternRefHostDataId {
let id = self.slab.alloc(value);
let id = ExternRefHostDataId(id);
log::trace!("allocated new externref host data: {id:?}");
id
}
pub fn dealloc(&mut self, id: ExternRefHostDataId) -> Box<dyn Any + Send + Sync> {
log::trace!("deallocated externref host data: {id:?}");
self.slab.dealloc(id.0)
}
pub fn get(&self, id: ExternRefHostDataId) -> &(dyn Any + Send + Sync) {
let data: &Box<dyn Any + Send + Sync> = self.slab.get(id.0).unwrap();
deref_box(data)
}
pub fn get_mut(&mut self, id: ExternRefHostDataId) -> &mut (dyn Any + Send + Sync) {
let data: &mut Box<dyn Any + Send + Sync> = self.slab.get_mut(id.0).unwrap();
deref_box_mut(data)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn correct_dyn_object() {
let mut table = ExternRefHostDataTable::default();
let x = 42_u32;
let id = table.alloc(Box::new(x));
assert!(table.get(id).is::<u32>());
assert_eq!(*table.get(id).downcast_ref::<u32>().unwrap(), 42);
assert!(table.get_mut(id).is::<u32>());
assert_eq!(*table.get_mut(id).downcast_ref::<u32>().unwrap(), 42);
}
}