Skip to content

Commit 305a3d6

Browse files
committed
feat: trait for inspectors that accumulate output
1 parent dc487dc commit 305a3d6

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/inspectors/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ pub use set::InspectorSet;
1010
mod spanning;
1111
pub use spanning::SpanningInspector;
1212

13+
mod with_output;
14+
pub use with_output::InspectorWithOutput;
15+
1316
#[cfg(test)]
1417
mod test {
1518
use super::*;

src/inspectors/with_output.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use crate::{helpers::Ctx, Trevm};
2+
use revm::{inspector::CountInspector, Database};
3+
4+
/// An inspector that can produce an output value after EVM execution.
5+
pub trait InspectorWithOutput<CTX>: revm::inspector::Inspector<CTX> {
6+
/// The type of output produced by the inspector.
7+
type Output;
8+
9+
/// Returns `true` if the inspector has a valid output value.
10+
fn has_output(&self) -> bool;
11+
12+
/// Reset the output value to the default state, discarding any current
13+
/// value.
14+
fn reset_output(&mut self) {
15+
self.take_output();
16+
}
17+
18+
/// Take the output value from the inspector, resetting it to the default
19+
/// state.
20+
fn take_output(&mut self) -> Self::Output;
21+
}
22+
23+
impl<Db: Database, Insp> Trevm<Db, Insp>
24+
where
25+
Insp: InspectorWithOutput<Ctx<Db>>,
26+
{
27+
/// Take the output value from the inspector.
28+
///
29+
/// This will also reset the output value in the inspector.
30+
pub fn take_output(&mut self) -> Insp::Output {
31+
self.inspector_mut().take_output()
32+
}
33+
}
34+
35+
impl<Ctx> InspectorWithOutput<Ctx> for CountInspector {
36+
type Output = Self;
37+
38+
fn has_output(&self) -> bool {
39+
self.total_opcodes() > 0
40+
}
41+
42+
fn reset_output(&mut self) {
43+
self.clear();
44+
}
45+
46+
fn take_output(&mut self) -> Self::Output {
47+
std::mem::take(self)
48+
}
49+
}

0 commit comments

Comments
 (0)