externref: Do not impl common traits, have free functions instead

If you aren't expecting `VMExternRef`'s pointer-equality semantics, then these
trait implementations can be foot guns. Instead of implementing the trait, make
free functions in the `VMExternRef` namespace. This way, callers have to be a
little more explicit.
This commit is contained in:
Nick Fitzgerald
2020-06-01 14:17:04 -07:00
parent 98da9ee8a9
commit b78eafcfd3
3 changed files with 53 additions and 29 deletions

View File

@@ -48,7 +48,7 @@ impl ExternRef {
/// This is *only* pointer equality, and does *not* run any inner value's
/// `Eq` implementation.
pub fn ptr_eq(&self, other: &ExternRef) -> bool {
self.inner == other.inner
VMExternRef::eq(&self.inner, &other.inner)
}
/// Returns the host information for this `externref`, if previously created

View File

@@ -8,6 +8,7 @@ use std::cmp;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::path::Path;
use std::rc::{Rc, Weak};
use std::sync::Arc;
@@ -736,7 +737,26 @@ pub(crate) struct StoreInner {
compiler: RefCell<Compiler>,
instances: RefCell<Vec<InstanceHandle>>,
signal_handler: RefCell<Option<Box<SignalHandler<'static>>>>,
host_info: RefCell<HashMap<VMExternRef, Rc<RefCell<dyn Any>>>>,
host_info: RefCell<HashMap<HostInfoKey, Rc<RefCell<dyn Any>>>>,
}
struct HostInfoKey(VMExternRef);
impl PartialEq for HostInfoKey {
fn eq(&self, rhs: &Self) -> bool {
VMExternRef::eq(&self.0, &rhs.0)
}
}
impl Eq for HostInfoKey {}
impl Hash for HostInfoKey {
fn hash<H>(&self, hasher: &mut H)
where
H: Hasher,
{
VMExternRef::hash(&self.0, hasher);
}
}
impl Store {
@@ -825,7 +845,7 @@ impl Store {
"externref must be from this store"
);
let infos = self.inner.host_info.borrow();
infos.get(&externref.inner).cloned()
infos.get(&HostInfoKey(externref.inner.clone())).cloned()
}
pub(crate) fn set_host_info(
@@ -839,9 +859,9 @@ impl Store {
);
let mut infos = self.inner.host_info.borrow_mut();
if let Some(info) = info {
infos.insert(externref.inner.clone(), info)
infos.insert(HostInfoKey(externref.inner.clone()), info)
} else {
infos.remove(&externref.inner)
infos.remove(&HostInfoKey(externref.inner.clone()))
}
}