Skip to content

Commit

Permalink
Merge pull request #3586 from wasmerio/as-engine-ref
Browse files Browse the repository at this point in the history
Make AsStoreRef and friends work for anything that implements Deref
  • Loading branch information
Michael Bryan authored Feb 15, 2023
2 parents b1a94df + 8d46f7f commit 73d6c48
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
37 changes: 19 additions & 18 deletions lib/api/src/sys/store.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<'_> {
Expand Down Expand Up @@ -374,21 +370,26 @@ impl AsStoreMut for StoreMut<'_> {
}
}

impl<T: AsStoreRef> AsStoreRef for &'_ T {
impl<P> 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<T: AsStoreRef> AsStoreRef for &'_ mut T {
fn as_store_ref(&self) -> StoreRef<'_> {
T::as_store_ref(*self)
}
}
impl<T: AsStoreMut> AsStoreMut for &'_ mut T {

impl<P> 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()
}
}
12 changes: 12 additions & 0 deletions lib/compiler/src/engine/engineref.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::ops::Deref;

use super::Engine;
use crate::Tunables;

Expand Down Expand Up @@ -37,3 +39,13 @@ impl AsEngineRef for EngineRef<'_> {
EngineRef { inner: self.inner }
}
}

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

0 comments on commit 73d6c48

Please sign in to comment.