What does the 'static lifetime mean in a trait bound in a Rust future? [duplicate]

青春壹個敷衍的年華 提交于 2019-12-01 08:31:52

问题


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.

来源:https://stackoverflow.com/questions/48504103/what-does-the-static-lifetime-mean-in-a-trait-bound-in-a-rust-future

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!