Some operations have traits that can effectively be used to respond when operations are called, like below:
use std::ops;
#[derive(Debug)]
struct MyNum(u32);
impl ops::Add for MyNum {
type Output = Self;
fn add(self, b: Self) -> Self {
println!("Added {} and {}", self.0, b.0);
MyNum(self.0 + b.0)
}
}
fn main() {
let a = MyNum(1);
let b = MyNum(2);
let c = a + b;
println!("C = {}", c.0);
}
However, as the docs state:
since the assignment operator (
=
) has no backing trait, there is no way of overloading its semantics
I want to be able to listen to when values are mutated via the assignment operator. The idea being to develop something similar to the Svelte library but in Rust. What would be the best approach to capture where variables are mutated - ideally keeping any overhead in the compile stage?