This is of course a simplified version of my actual problem.
fn my_func<'a, 'b>(a: &'a i32, b: &'b i32) -> i32 {
a + b
}
fn my_func_generic<'a, 'b, T>(a: &'a T, b: &'b T) -> T
where
&'a T: std::ops::Add<&'b T, Output = T>,
{
a + b
}
fn apply_function(x: &i32, y: &i32, function: fn(&i32, &i32) -> i32) -> i32 {
function(x, y)
}
fn main() {
let x = 10;
let y = 11;
println!("{}", my_func(&x, &y));
println!("{}", my_func_generic(&x, &y));
println!("{}", apply_function(&x, &y, my_func));
println!("{}", apply_function(&x, &y, my_func_generic));
}
error[E0308]: mismatched types
--> src/main.rs:23:43
|
23 | println!("{}", apply_function(&x, &y, my_func_generic));
| ^^^^^^^^^^^^^^^ one type is more general than the other
|
= note: expected fn pointer `for<'r, 's> fn(&'r i32, &'s i32) -> _`
found fn pointer `fn(&i32, &i32) -> _`
The question I have is: How can I use my_func_generic as a function pointer to pass it into apply_function?
It can definitely be used as such a function, as println!("{}", my_func_generic(&x, &y)) demonstrates.
Trying to write it in a pointer gives the same error:
let function_pointer: fn(&i32, &i32) -> i32 = my_func_generic;
Annotating generic types manually gives the same error:
let function_pointer: fn(&i32, &i32) -> i32 = my_func_generic::<'_, '_, i32>;
Sadly I don't understand the error message, and don't have enough knowledge about the inner type system of Rust generics to understand how I could solve this issue, or if it's simply not possible due to some mechanism yet unknown to me.