revm_context/
evm.rs

1use crate::setters::ContextSetters;
2use core::ops::{Deref, DerefMut};
3
4pub struct Evm<CTX, INSP, I, P> {
5    pub data: EvmData<CTX, INSP>,
6    pub instruction: I,
7    pub precompiles: P,
8}
9
10pub struct EvmData<CTX, INSP> {
11    pub ctx: CTX,
12    pub inspector: INSP,
13}
14
15impl<CTX> Evm<CTX, (), (), ()> {
16    pub fn new(ctx: CTX) -> Self {
17        Evm {
18            data: EvmData { ctx, inspector: () },
19            instruction: (),
20            precompiles: (),
21        }
22    }
23}
24
25impl<CTX: ContextSetters, INSP, I, P> Evm<CTX, INSP, I, P> {
26    /// Consumed self and returns new Evm type with given Inspector.
27    pub fn with_inspector<OINSP>(self, inspector: OINSP) -> Evm<CTX, OINSP, I, P> {
28        Evm {
29            data: EvmData {
30                ctx: self.data.ctx,
31                inspector,
32            },
33            instruction: self.instruction,
34            precompiles: self.precompiles,
35        }
36    }
37
38    /// Consumes self and returns new Evm type with given Precompiles.
39    pub fn with_precompiles<OP>(self, precompiles: OP) -> Evm<CTX, INSP, I, OP> {
40        Evm {
41            data: self.data,
42            instruction: self.instruction,
43            precompiles,
44        }
45    }
46
47    /// Consumes self and returns inner Inspector.
48    pub fn into_inspector(self) -> INSP {
49        self.data.inspector
50    }
51}
52
53impl<CTX: ContextSetters, INSP, I, P> ContextSetters for Evm<CTX, INSP, I, P> {
54    type Tx = <CTX as ContextSetters>::Tx;
55    type Block = <CTX as ContextSetters>::Block;
56
57    fn set_tx(&mut self, tx: Self::Tx) {
58        self.data.ctx.set_tx(tx);
59    }
60
61    fn set_block(&mut self, block: Self::Block) {
62        self.data.ctx.set_block(block);
63    }
64}
65
66impl<CTX, INSP, I, P> Deref for Evm<CTX, INSP, I, P> {
67    type Target = CTX;
68
69    fn deref(&self) -> &Self::Target {
70        &self.data.ctx
71    }
72}
73
74impl<CTX, INSP, I, P> DerefMut for Evm<CTX, INSP, I, P> {
75    fn deref_mut(&mut self) -> &mut Self::Target {
76        &mut self.data.ctx
77    }
78}