如何将流向量映射到 peekables?
How do I map a vector of streams to peekables?
我有 N futures::Stream
,其中每个 Item
都有一个时间戳。我想将它们合并排序为单个流,所以我想将所有流转换为 peekables:
fn merge_streams(
mut streams: Vec<Box<dyn futures::Stream<Item = Data>>>,
) -> impl futures::Stream<Item = Data> {
async_stream::try_stream! {
let n = streams.len();
let streams: Vec<_> = streams.into_iter().map(|s| s.peekable()).collect();
}
}
我被这个错误困住了:
error: the `peekable` method cannot be invoked on a trait object
|
| let streams: Vec<_> = streams.into_iter().map(|s| s.peekable()).collect();
peekable()
需要 Self: Sized
.
这是否意味着 T.peekable()
和 Box<T>.peekable
需要在编译时知道 sizeof(T)
?如果是这样,为什么?什么是解决方法?
我试过了
Vec<Box<dyn Stream>>
Vec<Box<dyn Stream + Sized>>
Vec<Box<_>>
Vec<&mut dyn Stream>
Vec<&mut dyn Stream + Sized>
使用BoxStream
(easily created by StreamExt::boxed
):
use futures::{StreamExt, stream::BoxStream}; // 0.3.5
fn merge_streams(streams: Vec<BoxStream<()>>) {
streams.into_iter().map(|s| s.peekable()).count();
}
我有 N futures::Stream
,其中每个 Item
都有一个时间戳。我想将它们合并排序为单个流,所以我想将所有流转换为 peekables:
fn merge_streams(
mut streams: Vec<Box<dyn futures::Stream<Item = Data>>>,
) -> impl futures::Stream<Item = Data> {
async_stream::try_stream! {
let n = streams.len();
let streams: Vec<_> = streams.into_iter().map(|s| s.peekable()).collect();
}
}
我被这个错误困住了:
error: the `peekable` method cannot be invoked on a trait object
|
| let streams: Vec<_> = streams.into_iter().map(|s| s.peekable()).collect();
peekable()
需要 Self: Sized
.
这是否意味着 T.peekable()
和 Box<T>.peekable
需要在编译时知道 sizeof(T)
?如果是这样,为什么?什么是解决方法?
我试过了
Vec<Box<dyn Stream>>
Vec<Box<dyn Stream + Sized>>
Vec<Box<_>>
Vec<&mut dyn Stream>
Vec<&mut dyn Stream + Sized>
使用BoxStream
(easily created by StreamExt::boxed
):
use futures::{StreamExt, stream::BoxStream}; // 0.3.5
fn merge_streams(streams: Vec<BoxStream<()>>) {
streams.into_iter().map(|s| s.peekable()).count();
}