diff --git a/lib/api/src/js/function_env.rs b/lib/api/src/js/function_env.rs index 81ef590a7c2..1705f1a4713 100644 --- a/lib/api/src/js/function_env.rs +++ b/lib/api/src/js/function_env.rs @@ -10,7 +10,7 @@ use crate::js::{AsStoreMut, AsStoreRef, StoreMut, StoreRef}; /// The function environment data is owned by the `Store`. pub struct FunctionEnv { pub(crate) handle: StoreHandle, - _phantom: PhantomData, + marker: PhantomData, } impl FunctionEnv { @@ -24,14 +24,14 @@ impl FunctionEnv { store.as_store_mut().objects_mut(), VMFunctionEnvironment::new(value), ), - _phantom: PhantomData, + marker: PhantomData, } } pub(crate) fn from_handle(handle: StoreHandle) -> Self { Self { handle, - _phantom: PhantomData, + marker: PhantomData, } } @@ -71,11 +71,26 @@ impl FunctionEnv { } } +impl PartialEq for FunctionEnv { + fn eq(&self, other: &Self) -> bool { + self.handle == other.handle + } +} + +impl Eq for FunctionEnv {} + +impl std::hash::Hash for FunctionEnv { + fn hash(&self, state: &mut H) { + self.handle.hash(state); + self.marker.hash(state); + } +} + impl Clone for FunctionEnv { fn clone(&self) -> Self { Self { handle: self.handle.clone(), - _phantom: self._phantom, + marker: self.marker, } } } diff --git a/lib/api/src/js/store.rs b/lib/api/src/js/store.rs index 59bc2577fd5..c4bd1c6ace5 100644 --- a/lib/api/src/js/store.rs +++ b/lib/api/src/js/store.rs @@ -202,7 +202,7 @@ mod objects { /// Every handle to an object managed by a context also contains the ID of the /// context. This is used to check that a handle is always used with the /// correct context. - #[derive(Debug, Copy, Clone, Eq, PartialEq)] + #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub struct StoreId(NonZeroU64); impl Default for StoreId { @@ -298,6 +298,14 @@ mod objects { self.id == other.id } } + + impl std::hash::Hash for StoreHandle { + fn hash(&self, state: &mut H) { + self.id.hash(state); + self.internal.idx.hash(state); + } + } + impl Clone for StoreHandle { fn clone(&self) -> Self { Self { diff --git a/lib/api/src/sys/function_env.rs b/lib/api/src/sys/function_env.rs index 0dde57ea78c..deb5c0b4304 100644 --- a/lib/api/src/sys/function_env.rs +++ b/lib/api/src/sys/function_env.rs @@ -10,11 +10,11 @@ use crate::{AsStoreMut, AsStoreRef, StoreMut, StoreRef}; /// The function environment data is owned by the `Store`. pub struct FunctionEnv { pub(crate) handle: StoreHandle, - _phantom: PhantomData, + marker: PhantomData, } impl FunctionEnv { - /// Make a new extern reference + /// Make a new FunctionEnv pub fn new(store: &mut impl AsStoreMut, value: T) -> Self where T: Any + Send + 'static + Sized, @@ -24,7 +24,7 @@ impl FunctionEnv { store.as_store_mut().objects_mut(), VMFunctionEnvironment::new(value), ), - _phantom: PhantomData, + marker: PhantomData, } } @@ -64,11 +64,26 @@ impl FunctionEnv { } } +impl PartialEq for FunctionEnv { + fn eq(&self, other: &Self) -> bool { + self.handle == other.handle + } +} + +impl Eq for FunctionEnv {} + +impl std::hash::Hash for FunctionEnv { + fn hash(&self, state: &mut H) { + self.handle.hash(state); + self.marker.hash(state); + } +} + impl Clone for FunctionEnv { fn clone(&self) -> Self { Self { handle: self.handle.clone(), - _phantom: self._phantom, + marker: self.marker, } } } diff --git a/lib/vm/src/store.rs b/lib/vm/src/store.rs index 747154f9d50..4faee579a4d 100644 --- a/lib/vm/src/store.rs +++ b/lib/vm/src/store.rs @@ -16,7 +16,7 @@ use crate::{InstanceHandle, VMFunction, VMFunctionEnvironment, VMGlobal, VMMemor /// Every handle to an object managed by a context also contains the ID of the /// context. This is used to check that a handle is always used with the /// correct context. -#[derive(Debug, Copy, Clone, Eq, PartialEq)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub struct StoreId(NonZeroU64); impl Default for StoreId { @@ -116,6 +116,13 @@ impl Clone for StoreHandle { } } +impl std::hash::Hash for StoreHandle { + fn hash(&self, state: &mut H) { + self.id.hash(state); + self.internal.idx.hash(state); + } +} + impl fmt::Debug for StoreHandle { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("StoreHandle")