fev*_*nym 5 lifetime rust rust-tokio
我以为我已经得到了一生'static,但现在我不确定了。
我正在尝试了解如何与 Tokio 和 Futures 合作。我的应用程序正在运行,但结构很糟糕,所以我需要分解它。这是'static我关闭的要求,我不知道如何解决。
例如,在一个main函数中,我有一个句柄并且能够spawn对其循环进行 future 操作:
let mut core = tokio_core::reactor::Core::new().unwrap();
let handle = core.handle();
let future = {
ok(())
};
Run Code Online (Sandbox Code Playgroud)
它编译;现在我想移出一些逻辑:
struct Test;
impl Test {
fn test(&self, handle: tokio_core::reactor::Handle) {
let future = {
ok(())
};
handle.spawn(future);
}
}
Run Code Online (Sandbox Code Playgroud)
它也能编译。当我的结构变得更加复杂时:
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));
}
}
Run Code Online (Sandbox Code Playgroud)
...我遇到了
let mut core = tokio_core::reactor::Core::new().unwrap();
let handle = core.handle();
let future = {
ok(())
};
Run Code Online (Sandbox Code Playgroud)
我认为它大喊大叫我的关闭。main如果我将它放在我的Arc'ed函数中,同样的 future 也会起作用Api。
将其移动到带有引用(例如单独的“处理器”)的结构会因'static错误而破坏它。
| 归档时间: |
|
| 查看次数: |
4904 次 |
| 最近记录: |