I have a noob question
A lifetime bound of
'static
is being elided on a generic closure somewhere. How would I specify the lifetime can be shorter? I only need the closure arg(s) to outlive the local context
variable in run_with_context
struct Context<'i> {
functions: Vec<Box<dyn FnOnce(&Context) + 'i>>
}
impl<'i> Context<'i> {
fn new() -> Self {
Self{functions: vec![]}
}
fn push_func<F: FnOnce(&Context) + 'i>(&mut self, f: F) {
self.functions.push(Box::new(f))
}
}
fn run_with_context<F: FnOnce(&mut Context)>(f: F) {
let mut context = Context::new();
f(&mut context);
//the context would normally do something here, but just drop it for simplicity
}
fn main() {
let my_string = "hello".to_string();
run_with_context(|context| context.push_func(|context| println!("{}", &my_string)));
}
Any pointers are appreciated.