#[evaluate]
Expand description
Evaluates a zero-argument function and optionally reports when the value changes.
The #[evaluate(print_fn)]
attribute macro transforms a function into a reactive
evaluator that:
- Computes the function result on each call.
- Compares it with the previously computed value.
- If the value is unchanged, calls the specified print function with a message.
§Requirements
- The function must have no parameters.
- The function must return a value (
-> T
), which must implementEq + Clone
. - The print function (e.g.,
print
) must be a callable accepting aString
.
§Examples
use reactive_macros::evaluate;
fn print(msg: String) {
println!("{}", msg);
}
#[evaluate(print)]
pub fn get_number() -> i32 {
42
}
fn main() {
// First call computes the value
assert_eq!(get_number(), 42);
// Second call compares with previous; prints message since value didn't change
assert_eq!(get_number(), 42);
}
§SAFETY
This macro uses a static mut
internally to store the previous value,
so it is not thread-safe. It should only be used in single-threaded contexts.