I think what I need to do is refer to a lifetime that is inside a function , but I can't figure it out.
Basically I have many structs with a lifetime with a couple of methods.
struct Example<'a> {
..
}
impl<'a> Example<'a> {
fn new() -> Self { .. }
fn example(&self, arg: &'a str) { .. }
}
I want to add an abstraction layer by using a trait for these methods
trait Behaviour<'a> {
fn new() -> Self;
fn behaviour(&self, arg: &'a str);
}
This all works fine until I started using it, I tried to write a generic function like this.
fn run<'a, B: Behaviour<'a>>(r: &mut Runner) {
r.run(|ctx| {
let arg = String::from("test");
let ex = B::new();
ctx.frobnicate(|| ex.behaviour(&arg));
});
}
And I get the following error
| fn run<'a, B: Behaviour<'a>>(r: &mut Runner) {
| -- lifetime `'a` defined here
| ctx.frobnicate(|| ex.behaviour(&arg));
| -- --------------^^^-
| | | |
| | | borrowed value does not live long enough
| | argument requires that `arg` is borrowed for `'a`
| value captured here
| });
| - `arg` dropped here while still borrowed
Is there anything I can do? I am able to modify the trait and the calling code but I can't change the Runner
or the Example
struct.