Hello guys, I 'v got this little issue about passing a method as parameter when a closure is required. There is the obvious solution of creating a closure which just redirects to the method call, but I just wonder if there is a better approach.
If I declare a function like this:
fn apply(x: i64, y: i64, callback: &mut impl FnMut(i64, i64) -> ()) {
callback(x, y);
}
I have a couple of valid options to pass the callback parameter to it such:
apply(100i64, 100i64, &mut |x, y| println!("Closure {} {}", x, y));
apply(100i64, 100i64, &mut SomeHandler::some_handler);
apply(100i64, 100i64, &mut some_func);
However I would like to pass to it a struct method. For example if I have something like:
#[derive(Default)]
pub struct SumHandler {
sum: i64,
}
impl SumHandler {
pub fn sum_handler(&mut self, x: i64, y: i64) {
self.sum += x;
self.sum += y;
println!("Sum handler {} {}", x, y);
}
}
I could call it like this:
fn main() {
let mut sum_h = SumHandler::default();
//works but is does an indirection
apply(100i64, 100i64, &mut |x, y| sum_h.sum_handler(x, y));
}
However this creates a closure which calls my method, instead to refer the method directly.
Is it anyway to refer that method from the given struct instance directly?
Something in the line of (None of this do work):
//not working but I would like something like this
//apply(100i64, 100i64,sum_h::sum_handler);
//apply(100i64, 100i64,sum_h.sum_handler);
//apply(100i64, 100i64,SumHandler::sum_h::sum_handler);
//apply(100i64, 100i64,SumHandler::sum_handler(&sum_h));
You can check the full code on rust playground here
Thanks a lot!