Skip to content

Commit b83a678

Browse files
authored
chore: add some DynPrompile impls (#117)
1 parent f884e75 commit b83a678

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

crates/evm/src/precompiles.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use revm::{
1010
context::{Cfg, ContextTr, LocalContextTr},
1111
handler::{EthPrecompiles, PrecompileProvider},
1212
interpreter::{CallInput, Gas, InputsImpl, InstructionResult, InterpreterResult},
13-
precompile::{PrecompileError, PrecompileResult, Precompiles},
13+
precompile::{PrecompileError, PrecompileFn, PrecompileResult, Precompiles},
1414
};
1515

1616
/// A mapping of precompile contracts that can be either static (builtin) or dynamic.
@@ -284,6 +284,16 @@ impl<CTX: ContextTr> PrecompileProvider<CTX> for PrecompilesMap {
284284
#[derive(Clone)]
285285
pub struct DynPrecompile(pub(crate) Arc<dyn Precompile + Send + Sync>);
286286

287+
impl DynPrecompile {
288+
/// Creates a new [`DynPrecompiles`] with the given closure.
289+
pub fn new<F>(f: F) -> Self
290+
where
291+
F: Fn(PrecompileInput<'_>) -> PrecompileResult + Send + Sync + 'static,
292+
{
293+
Self(Arc::new(f))
294+
}
295+
}
296+
287297
impl core::fmt::Debug for DynPrecompile {
288298
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
289299
f.debug_struct("DynPrecompile").finish()
@@ -345,6 +355,13 @@ where
345355
}
346356
}
347357

358+
impl From<PrecompileFn> for DynPrecompile {
359+
fn from(f: PrecompileFn) -> Self {
360+
let p = move |input: PrecompileInput<'_>| f(input.data, input.gas);
361+
p.into()
362+
}
363+
}
364+
348365
impl Precompile for DynPrecompile {
349366
fn call(&self, input: PrecompileInput<'_>) -> PrecompileResult {
350367
self.0.call(input)

0 commit comments

Comments
 (0)