How to implement Solidity modifier functions in Rust?

Viewed 137

Solidity modifiers are syntactic sugar to add code around functions. Modifiers are reusable across in multiple functions, reducing repetitive checks.

modifier lock() {
    require(slot0.unlocked, 'LOK');
    slot0.unlocked = false;
    _; // run function here
    slot0.unlocked = true;
}

Use it as follows:

function my_function() external override lock returns (uint256 some_val) {}

How to implement a similar pattern in rust? Currently I'm adding the required code directly inside my functions. But this causes a lot of repetition.

fn my_function() -> u8 {
    // modifier pre code
 
    // My function code here ....

    // modifier post code
}
0 Answers
Related