This question already has an answer here:
- Why does the Rust compiler request I constrain a generic type parameter's lifetime (error E0309)? 1 answer
- Why is the bound `T: 'a` required in order to store a reference `&'a T`? 2 answers
- The compiler suggests I add a 'static lifetime because the parameter type may not live long enough, but I don't think that's what I want 2 answers
I thought that I've got what the 'static
lifetime is, but now I'm not sure.
I'm trying to understand how to work with Tokio and Futures. My app is working, but the structure is awful, so I need to decompose it. Here comes 'static
requirement for my closures, that I don't know how to resolve.
For example, in a main
function I have a handle and able to spawn
a future to its loop:
let mut core = tokio_core::reactor::Core::new().unwrap();
let handle = core.handle();
let future = {
ok(())
};
It compiles; now I want to move some logic out:
struct Test;
impl Test {
fn test(&self, handle: tokio_core::reactor::Handle) {
let future = {
ok(())
};
handle.spawn(future);
}
}
It compiles too. When my struct gets more complicated:
struct Test<'a> {
api: &'a Arc<Api>,
}
impl<'a> Test<'a> {
fn new(api: &'a Arc<Api>) -> Self {
Test {
api: api,
}
}
fn test(&self, msg: &telegram_bot::types::Message, handle: tokio_core::reactor::Handle) {
let api = self.api.clone();
let future = ok(10);
let future2 = move |x| {
api.send(msg.text_reply(format!("reply: {}", x))) // returns Future
}.map(|_| ()).map_err(|_| ()); // let's ignore the results for now
handle.spawn(future.and_then(future2));
}
}
... I run into
error[E0477]: the type `futures::AndThen<futures::FutureResult<i32, ()>, futures::FutureResult<(), ()>, [closure@src/main.rs:63:23: 66:10 api:std::sync::Arc<telegram_bot::Api>, msg:&telegram_bot::Message]>` does not fulfill the required lifetime
handle.spawn(future.and_then(future2));
^^^^^
note: type must satisfy the static lifetime
I think it yells about my closure. The same future works if I have it in the main
function with my Arc
'ed Api
.
Moving this to a struct with references like a separate "processor" break it with 'static
errors.