Rust 无法tokio::运行已装箱的未来,因为无法满足trait绑定的发送

Rust 无法tokio::运行已装箱的未来,因为无法满足trait绑定的发送,rust,rust-tokio,Rust,Rust Tokio,我有一个函数,根据参数的不同,它可以选择运行未来或不执行任何操作。我试着在将要返回的两个期货周围放置一个框,一个tokio::prelude::future::Done,它立即解析为Ok(()),一个tokio::timer::Delay,我正在使用和Box{ 如果x==0{ 盒子:新的( 计时器::延迟::新建(即时::现在()+持续时间::从秒(5)) .然后(| |好(())) .map_err(| |()), ) }否则{ 框::新(未来::结果(确定(())) } } 编译失败,错误

我有一个函数,根据参数的不同,它可以选择运行未来或不执行任何操作。我试着在将要返回的两个期货周围放置一个
,一个
tokio::prelude::future::Done
,它立即解析为
Ok(())
,一个
tokio::timer::Delay
,我正在使用
map\u err
项和
错误转换为
()
。当我尝试使用
tokio::run
运行期货时,这似乎对我不起作用

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(())))
    }
}
extern板条箱东京;
使用std::time::{Duration,Instant};
使用东京::前奏::*;
使用tokio::定时器;
fn main(){
东京:运行(foo(12));
}
fn foo(x:i32)->Box{
如果x==0{
盒子:新的(
计时器::延迟::新建(即时::现在()+持续时间::从秒(5))
.然后(| |好(()))
.map_err(| |()),
)
}否则{
框::新(未来::结果(确定(()))
}
}
编译失败,错误消息如下:

error[E0277]:特性绑定'tokio::prelude::Future:std::marker::Send'不满足
-->src/main.rs:8:5
|
8 |东京:运行(foo(12));
|^^^^^^^^`tokio::prelude::Future`无法在线程之间安全发送
|
=help:trait`std::marker::Send`未为`tokio::prelude::Future'实现`
=注意:由于'std::marker::Send'的impl中对'std::ptr::Unique'的要求,因此需要`
=注意:必需,因为它出现在类型“std::boxed::Box”中`
=注意:`tokio::run'需要`

似乎
没有实现
发送
,这对我来说没有意义。由于我返回的
Future
类型都是implement
Send
,我认为
框应该是,因为
impl Send for框中的T:Send
是stdlib中的自动实现。我在这里遗漏了什么?

我意识到我需要在
Foo
的返回类型中指定未来是
Send
。这包括:

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 = ()> + Send> { // note the + Send at the end of this line
    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(())))
    }
}
extern板条箱东京;
使用std::time::{Duration,Instant};
使用东京::前奏::*;
使用tokio::定时器;
fn main(){
东京:运行(foo(12));
}
fn foo(x:i32)->Box{//注意此行末尾的+Send
如果x==0{
盒子:新的(
计时器::延迟::新建(即时::现在()+持续时间::从秒(5))
.然后(| |好(()))
.map_err(| |()),
)
}否则{
框::新(未来::结果(确定(()))
}
}

FWIW,在这种情况下,您可以直接使用。谢谢@Shepmaster!这正是我想要的。那比拳击好多了。