Skip to content

Commit 9eb7ca0

Browse files
committed
use with_context_data for matching logic
1 parent d966399 commit 9eb7ca0

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

packages/vm/src/vm.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::error::Error;
22

3-
use std::borrow::Borrow;
3+
use std::borrow::{Borrow, BorrowMut};
44
use std::ptr::NonNull;
55
use std::sync::{Arc, RwLock};
66
use wasmer::{Instance, Memory, WasmerEnv};
@@ -76,8 +76,9 @@ where
7676

7777
/// Creates a back reference from a contact to its partent instance
7878
pub fn set_wasmer_instance(&self, instance: Option<NonNull<Instance>>) {
79-
let mut data = self.data.as_ref().write().unwrap();
80-
data.wasmer_instance = instance;
79+
self.with_context_data_mut(|data| {
80+
data.wasmer_instance = instance;
81+
})
8182
}
8283

8384
pub fn with_wasmer_instance<C, R>(&self, callback: C) -> Result<R, Error>
@@ -102,14 +103,14 @@ where
102103
callback(context_data)
103104
}
104105

105-
// fn with_context_data_mut<C, R>(&self, callback: C) -> R
106-
// where
107-
// C: FnOnce(&mut ContextData<Q>) -> R,
108-
// {
109-
// let mut guard = self.data.as_ref().write().unwrap();
110-
// let context_data = guard.borrow_mut();
111-
// callback(context_data)
112-
// }
106+
fn with_context_data_mut<C, R>(&self, callback: C) -> R
107+
where
108+
C: FnOnce(&mut ContextData<Q>) -> R,
109+
{
110+
let mut guard = self.data.as_ref().write().unwrap();
111+
let context_data = guard.borrow_mut();
112+
callback(context_data)
113+
}
113114

114115
pub fn get_gas_left(&self) -> u64 {
115116
self.with_wasmer_instance(|instance| {
@@ -140,8 +141,7 @@ where
140141
}
141142

142143
pub fn memory(&self) -> Result<Memory, Error> {
143-
let data = self.data.as_ref().read().unwrap();
144-
match data.wasmer_instance {
144+
self.with_context_data(|data| match data.wasmer_instance {
145145
Some(instance_ptr) => {
146146
let instance_ref = unsafe { instance_ptr.as_ref() };
147147
let mut memories: Vec<Memory> =
@@ -153,7 +153,7 @@ where
153153
}
154154
}
155155
_ => Err(Error::BadMemorySectionError),
156-
}
156+
})
157157
}
158158
}
159159

0 commit comments

Comments
 (0)