将BoxStream转换为BoxFuture

时间:2017-04-28 20:53:05

标签: compiler-errors rust

我有一个返回BoxStream<(), io::Error>的函数,并希望将此流转换为Future(或BoxFuture),但我遇到了一些编译问题:

extern crate futures;

use futures::stream::BoxStream;
use std::io;

pub fn foo() -> BoxStream<(), io::Error> {
    unimplemented!()
}

fn main() {
    let a = foo().into_future();
}

错误信息:

error[E0277]: the trait bound `futures::Stream<Error=std::io::Error, Item=()> + std::marker::Send + 'static: std::marker::Sized` is not satisfied
  --> src/main.rs:23:19
   |
23 |     let a = foo().into_future();
   |                   ^^^^^^^^^^^ the trait `std::marker::Sized` is not implemented for `futures::Stream<Error=std::io::Error, Item=()> + std::marker::Send + 'static`
   |
   = note: `futures::Stream<Error=std::io::Error, Item=()> + std::marker::Send + 'static` does not have a constant size known at compile-time

有解决方法吗?

1 个答案:

答案 0 :(得分:3)

您正在呼叫futures::future::IntoFuture::into_future,而不是futures::stream::Stream::into_future。您需要导入特征:

extern crate futures;

use futures::Stream; // This
use futures::stream::BoxStream;
use std::io;

pub fn foo() -> BoxStream<(), io::Error> {
    unimplemented!()
}

fn main() {
    let a = foo().into_future();
}

您可以使用

验证差异
fn main() {
    let a = futures::future::IntoFuture::into_future(foo());
    let a = futures::Stream::into_future(foo());
}