<分区>
我有一个函数,可以根据参数选择运行 future 或什么也不做。我试着放一个 Box
围绕将返回的两个 future ,一个tokio::prelude::future::Done<Item=(), Error=()>
立即解析为 Ok(())
, 和一个 tokio::timer::Delay
我正在使用 and_then
和 map_err
把Item
和 Error
至 ()
.当我尝试使用 tokio::run
运行 future 时,这似乎对我不起作用.
extern crate tokio;
use std::time::{Duration, Instant};
use tokio::prelude::*;
use tokio::timer;
fn main() {
tokio::run(foo(12));
}
fn foo(x: i32) -> Box<Future<Item = (), Error = ()>> {
if x == 0 {
Box::new(
timer::Delay::new(Instant::now() + Duration::from_secs(5))
.and_then(|_| Ok(()))
.map_err(|_| ()),
)
} else {
Box::new(future::result(Ok(())))
}
}
编译失败,错误信息如下:
error[E0277]: the trait bound `tokio::prelude::Future<Error=(), Item=()>: std::marker::Send` is not satisfied
--> src/main.rs:8:5
|
8 | tokio::run(foo(12));
| ^^^^^^^^^^ `tokio::prelude::Future<Error=(), Item=()>` cannot be sent between threads safely
|
= help: the trait `std::marker::Send` is not implemented for `tokio::prelude::Future<Error=(), Item=()>`
= note: required because of the requirements on the impl of `std::marker::Send` for `std::ptr::Unique<tokio::prelude::Future<Error=(), Item=()>>`
= note: required because it appears within the type `std::boxed::Box<tokio::prelude::Future<Error=(), Item=()>>`
= note: required by `tokio::run`
看来 Box<Future...>
不执行 Send
,这对我来说没有意义。自 Future
我要返回的类型都实现了 Send
, 在我看来 Box
应该,因为impl Send for Box<T> where T: Send
是标准库中的一个自动工具。我在这里缺少什么?