Attribute Macro brush_lang::modifier_definition
source · [−]#[modifier_definition]
Expand description
This macro only checks that some free-standing function satisfies a set of rules.
Rules:
- First argument should not be
self
. - First argument must be a reference to a type
instance: &T
. In most cases it’s the instance of contract. - Second argument is function’s body(this function contains the main code of method attached to the modifier).
The type must be
Fn(&T)
,FnMut(&T)
orFnOnce(&T)
. - Every next argument should not be references to object. Because modifier allows only to pass arguments by value(Modifier will pass the clone of argument).
- The return type of body function(second argument) must be the same as the return type of modifier.
Example: Definition
#[derive(Default)]
struct Contract {
initialized: bool,
}
#[brush::modifier_definition]
fn once<BodyFn: FnOnce(&mut Contract)>(instance: &mut Contract, body: BodyFn, _example_data1: u8, _example_data2: u8) {
assert!(!instance.initialized, "Contract is already initialized");
body(instance);
instance.initialized = true;
}