Skip to content

Commit 65096c1

Browse files
authored
docs: improve apply_precompile documentation (#106)
Added comprehensive documentation for the apply_precompile method that explains its behavior for adding, updating, and removing precompiles. Key improvements: - Clarified that the method applies a transformation to existing precompiles - Explained the closure's input (Option<precompile>) and output behavior - Added clear examples for common use cases: - Adding a new precompile - Updating an existing precompile - Removing a precompile - Conditional operations - Documented that returning None removes the precompile - Documented that returning Some inserts or replaces the precompile This makes the method's behavior much clearer, especially the fact that it can be used to remove precompiles by returning None from the closure.
1 parent 4c4405d commit 65096c1

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

crates/evm/src/precompiles.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,44 @@ impl PrecompilesMap {
7272
dyn_precompiles.inner = new_map;
7373
}
7474

75-
/// Applies a new precompile at the given address.
75+
/// Applies a transformation to the precompile at the given address.
76+
///
77+
/// This method allows you to add, update, or remove a precompile by applying a closure
78+
/// to the existing precompile (if any) at the specified address.
79+
///
80+
/// # Behavior
81+
///
82+
/// The closure receives:
83+
/// - `Some(precompile)` if a precompile exists at the address
84+
/// - `None` if no precompile exists at the address
85+
///
86+
/// Based on what the closure returns:
87+
/// - `Some(precompile)` - Insert or replace the precompile at the address
88+
/// - `None` - Remove the precompile from the address (if it exists)
89+
///
90+
/// # Examples
91+
///
92+
/// ```ignore
93+
/// // Add a new precompile
94+
/// precompiles.apply_precompile(&address, |_| Some(my_precompile));
95+
///
96+
/// // Update an existing precompile
97+
/// precompiles.apply_precompile(&address, |existing| {
98+
/// existing.map(|p| wrap_with_logging(p))
99+
/// });
100+
///
101+
/// // Remove a precompile
102+
/// precompiles.apply_precompile(&address, |_| None);
103+
///
104+
/// // Conditionally update
105+
/// precompiles.apply_precompile(&address, |existing| {
106+
/// if let Some(p) = existing {
107+
/// Some(modify_precompile(p))
108+
/// } else {
109+
/// Some(create_default_precompile())
110+
/// }
111+
/// });
112+
/// ```
76113
pub fn apply_precompile<F>(&mut self, address: &Address, f: F)
77114
where
78115
F: FnOnce(Option<DynPrecompile>) -> Option<DynPrecompile>,

0 commit comments

Comments
 (0)