Hi everyone,
I am writing a library whose users will be able to specify one function to compute some values and a second function to call on the first one. My library will provide a struct to hold both and simplify the calculations.
I am building this step by step. First, I write two standalone functions and this works:
fn double(x: f32) -> f32 {
x * 2.0
}
fn do_call<F: Fn(f32) -> f32>(f: F, x: f32) -> f32 {
f(x)
}
fn main() {
println!("{}", do_call(double, 3.0));
println!("{}", do_call(&double, 3.0)) //Note that & also works
}
Let's wrap this inside a struct:
fn dbl(x: f32) -> f32 {
x * 2.0
}
fn do_call<F: Fn(f32) -> f32>(f: F, x: f32) -> f32 {
f(x)
}
struct Lazy<F, G>
where F: Fn(f32) -> f32,
G: Fn(F, f32) -> f32
{
callee: F,
caller: G
}
impl<F, G> Lazy<F, G>
where F: Fn(f32) -> f32,
G: Fn(F, f32) -> f32
{
fn call(&self, x: f32) -> f32 {
(self.caller)(&self.callee, x)
}
}
fn main() {
let l = Lazy{callee: dbl, caller: do_call};
println!("{}", l.call(3.0));
}
This fails with:
error[E0308]: mismatched types
--> <anon>:22:23
|
22 | (self.caller)(&self.callee, x)
| ^^^^^^^^^^^^ expected type parameter, found &F
|
= note: expected type `F`
= note: found type `&F`
error: aborting due to previous error
Why does & not work now? The bounds on F are the same, as far as I can see.
Thanks.