Stream 返回流特征对象的未来

Stream 返回流特征对象的未来,stream,rust,future,traits,Stream,Rust,Future,Traits,我使用的是futures=“0.1.21”板条箱,我试图编写一个函数,返回一个trait对象,该对象是bools的流的未来。在实际的程序中,我正在建立一个到服务器的连接,该服务器定期流式传输其操作状态 期货 我已经能够返回bool“trait对象的”Future,如下所示: extern crate futures; use futures::{future, Future}; fn future() -> Box<Future<Item = bool, Error = st

我使用的是
futures=“0.1.21”
板条箱,我试图编写一个函数,返回一个trait对象,该对象是
bool
s的
流的
未来。在实际的程序中,我正在建立一个到服务器的连接,该服务器定期流式传输其操作状态

期货 我已经能够返回
bool
“trait对象的”
Future
,如下所示:

extern crate futures;
use futures::{future, Future};

fn future() -> Box<Future<Item = bool, Error = std::io::Error>> {
    Box::new(future::ok(true))
}

fn main() { future(); }
它无法通过以下方式编译:

error[E0271]:类型不匹配解决`::Item==futures::Stream`
-->src/main.rs:5:5
|
5 | Box::new(future::ok(stream::empty::()))
|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
=注意:应为类型`futures::stream::Empty`
找到类型“futures::Stream”`
=注意:强制转换为对象类型“futures::Future”时需要`
遍历器 如果我尝试返回嵌套的
迭代器,我会遇到类似的问题,例如:

fn iter2() -> Box<Iterator<Item = Iterator<Item = bool>>> {
    Box::new(vec![vec![true].into_iter()].into_iter())
}
fn iter2()->框{
框::新建(vec![vec![true]。插入iter()]。插入iter())
}
在以下情况下失败:

error[E0271]:类型不匹配解析“::Item==std::iter::Iterator`
-->src/main.rs:2:5
|
2 | Box::new(vec![vec![true].into_iter()]into_iter())
|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^预期结构“std::vec::IntoIter`,已找到trait std::iter::Iterator
|
=注意:应为'std::vec::IntoIter'类型`
找到类型“std::iter::Iterator”`
=注意:强制转换到对象类型'std::iter::Iterator'时需要`
其他选择? 我怀疑要么像这样“嵌套”特征是不可能的,要么我还没有弄清楚语法


如果不可能的话,有没有其他设计/模式可以让我完成这样的任务?

你的问题让我非常困惑。你似乎明白你需要1来包装未来,那么你为什么不将完全相同的逻辑应用于流呢

type BoxedStream = Box<Stream<Item = bool, Error = io::Error>>;

fn stream_future() -> Box<Future<Item = BoxedStream, Error = io::Error>> {
    let s: BoxedStream = Box::new(stream::empty());
    Box::new(future::ok(s))
}
type BoxedStream = Box<Stream<Item = bool, Error = io::Error>>;

fn stream_future() -> Box<Future<Item = BoxedStream, Error = io::Error>> {
    let s: BoxedStream = Box::new(stream::empty());
    Box::new(future::ok(s))
}
fn stream_future() -> impl Future<Item = impl Stream<Item = bool, Error = io::Error>, Error = io::Error> {
    future::ok(stream::empty())
}