From 8d46f7f2d72ad8f62b44cf3c795ea14d6bd33759 Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Thu, 16 Feb 2023 00:41:03 +0800 Subject: [PATCH] Make AsStoreRef and friends work for anything that derefs to an AsStoreRef --- lib/api/src/sys/store.rs | 37 ++++++++++++++-------------- lib/compiler/src/engine/engineref.rs | 12 +++++++++ 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/lib/api/src/sys/store.rs b/lib/api/src/sys/store.rs index 6f7cf6ac9fd..9636bfa1163 100644 --- a/lib/api/src/sys/store.rs +++ b/lib/api/src/sys/store.rs @@ -1,6 +1,9 @@ use crate::sys::tunables::BaseTunables; use derivative::Derivative; -use std::fmt; +use std::{ + fmt, + ops::{Deref, DerefMut}, +}; #[cfg(feature = "compiler")] use wasmer_compiler::{AsEngineRef, Engine, EngineBuilder, EngineRef, Tunables}; use wasmer_types::OnCalledAction; @@ -220,13 +223,6 @@ impl AsEngineRef for Store { } } -#[cfg(feature = "compiler")] -impl AsEngineRef for &Store { - fn as_engine_ref(&self) -> EngineRef<'_> { - EngineRef::new(&self.engine) - } -} - #[cfg(feature = "compiler")] impl AsEngineRef for StoreRef<'_> { fn as_engine_ref(&self) -> EngineRef<'_> { @@ -374,21 +370,26 @@ impl AsStoreMut for StoreMut<'_> { } } -impl AsStoreRef for &'_ T { +impl

AsStoreRef for P +where + P: Deref, + P::Target: AsStoreRef, +{ fn as_store_ref(&self) -> StoreRef<'_> { - T::as_store_ref(*self) + (**self).as_store_ref() } } -impl AsStoreRef for &'_ mut T { - fn as_store_ref(&self) -> StoreRef<'_> { - T::as_store_ref(*self) - } -} -impl AsStoreMut for &'_ mut T { + +impl

AsStoreMut for P +where + P: DerefMut, + P::Target: AsStoreMut, +{ fn as_store_mut(&mut self) -> StoreMut<'_> { - T::as_store_mut(*self) + (**self).as_store_mut() } + fn objects_mut(&mut self) -> &mut StoreObjects { - T::objects_mut(*self) + (**self).objects_mut() } } diff --git a/lib/compiler/src/engine/engineref.rs b/lib/compiler/src/engine/engineref.rs index 107cd9b1153..f5fa32aa551 100644 --- a/lib/compiler/src/engine/engineref.rs +++ b/lib/compiler/src/engine/engineref.rs @@ -1,3 +1,5 @@ +use core::ops::Deref; + use super::Engine; use crate::Tunables; @@ -37,3 +39,13 @@ impl AsEngineRef for EngineRef<'_> { EngineRef { inner: self.inner } } } + +impl

AsEngineRef for P +where + P: Deref, + P::Target: AsEngineRef, +{ + fn as_engine_ref(&self) -> EngineRef<'_> { + (**self).as_engine_ref() + } +}