I fail to use function as argument when lifetimes need to be set. Below an example (playground):
use std::fmt::{Debug, Display};
#[derive(Default)]
pub struct Context {}
fn function_1<'a>(_: &'a Context,a: &'a mut String) -> &'a String {
a.push_str("1");
a
}
fn function_2<'a>(_: &'a Context, b : &'a mut usize) -> &'a usize {
*b += 1;
b
}
fn use_function_1_or_function_2<'a, D>(
d: D,
f: fn(&'a Context,&'a mut D) -> &'a D,
) -> String
where D: Display
{
let context = Context::default();
let mut d = d;
let d = f(&context,&mut d);
format!("{}", d)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn simple_test() {
let string = "hello".to_string();
let result = use_function_1_or_function_2(string, function_1);
assert_eq!(result, "hello1");
}
}
Here the below error:
error[E0597]: `context` does not live long enough
--> src/generic_function_as_parameter.rs:25:15
|
17 | fn use_function_1_or_function_2<'a, D>(
| -- lifetime `'a` defined here
...
25 | let d = f(&context,&mut d);
| --^^^^^^^^--------
| | |
| | borrowed value does not live long enough
| argument requires that `context` is borrowed for `'a`
26 | format!("{}", d)
27 | }
| - `context` dropped here while still borrowed
I don't understand why it is a problem for the compilation that context
is dropped at the end of the method use_function_1_or_function_2
, we don't use it anymore.
I suppose that the compilator doesn't know the use of reference arguments because of the function is a dynamic callback.
There is some way to do it?