Skip to content

Commit ca6a5ea

Browse files
committed
feat: try_with_inspector
1 parent f665b78 commit ca6a5ea

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "trevm"
3-
version = "0.27.4"
3+
version = "0.27.5"
44
rust-version = "1.83.0"
55
edition = "2021"
66
authors = ["init4"]

src/evm.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,7 @@ where
113113
}
114114

115115
/// Replace the current inspector with a new inspector of the same type.
116-
pub fn replace_inspector(mut self, inspector: Insp) -> Self
117-
where
118-
TrevmState: Copy,
119-
{
116+
pub fn replace_inspector(mut self, inspector: Insp) -> Self {
120117
*self.inspector_mut() = inspector;
121118
self
122119
}
@@ -128,7 +125,6 @@ where
128125
) -> Trevm<Db, Layered<Insp2, Insp>, TrevmState>
129126
where
130127
Insp2: Inspector<Ctx<Db>>,
131-
TrevmState: Copy,
132128
{
133129
let inner = Box::new(Evm {
134130
ctx: self.inner.ctx,
@@ -158,7 +154,6 @@ where
158154
pub fn set_inspector<Insp2>(self, inspector: Insp2) -> Trevm<Db, Insp2, TrevmState>
159155
where
160156
Insp2: Inspector<Ctx<Db>>,
161-
TrevmState: Copy,
162157
{
163158
let inner = Box::new(Evm {
164159
ctx: self.inner.ctx,
@@ -180,14 +175,36 @@ where
180175
where
181176
Insp2: Inspector<Ctx<Db>>,
182177
F: FnOnce(Trevm<Db, Insp2, TrevmState>) -> Trevm<Db, Insp2, NewState>,
183-
TrevmState: Copy,
184-
NewState: Copy,
185178
{
186179
let (old, this) = self.take_inspector();
187180
let this = f(this.set_inspector(inspector));
188181
this.set_inspector(old)
189182
}
190183

184+
/// Run a fallible function with the provided inspector, then restore the
185+
/// previous inspector. If the function returns an error, it will be
186+
/// wrapped in an [`EvmErrored`] along with the current EVM state.
187+
pub fn try_with_inspector<C, F, Insp2, NewState, E>(
188+
self,
189+
inspector: Insp2,
190+
f: F,
191+
) -> Result<Trevm<Db, Insp, NewState>, EvmErrored<Db, Insp, E>>
192+
where
193+
C: Cfg,
194+
Insp2: Inspector<Ctx<Db>>,
195+
F: FnOnce(
196+
Trevm<Db, Insp2, TrevmState>,
197+
) -> Result<Trevm<Db, Insp, NewState>, EvmErrored<Db, Insp, E>>,
198+
{
199+
let (previous, this) = self.take_inspector();
200+
let this = this.set_inspector(inspector);
201+
202+
match f(this) {
203+
Ok(evm) => Ok(evm.set_inspector(previous)),
204+
Err(evm) => Err(evm.set_inspector(previous)),
205+
}
206+
}
207+
191208
/// Get the id of the currently running hardfork spec.
192209
pub fn spec_id(&self) -> SpecId {
193210
self.inner.ctx.cfg().spec()

0 commit comments

Comments
 (0)