Stream 一旦其中一个底层流耗尽,则使流组合排气
如果我想将多个相同类型的流合并为一个流,我将使用:Stream 一旦其中一个底层流耗尽,则使流组合排气,stream,rust,future,Stream,Rust,Future,如果我想将多个相同类型的流合并为一个流,我将使用: 然而,一旦其中一个流耗尽,另一个流仍然可以生成组合流的结果。在任何一个底层流耗尽后,我可以使用什么来耗尽组合流?编写您自己的流组合器: use futures::{Async, Poll, Stream}; // 0.1.25 struct WhileBoth<S1, S2>(S1, S2) where S1: Stream, S2: Stream<Item = S1::Item, Error = S1::E
然而,一旦其中一个流耗尽,另一个流仍然可以生成组合流的结果。在任何一个底层流耗尽后,我可以使用什么来耗尽组合流?编写您自己的流组合器:
use futures::{Async, Poll, Stream}; // 0.1.25
struct WhileBoth<S1, S2>(S1, S2)
where
S1: Stream,
S2: Stream<Item = S1::Item, Error = S1::Error>;
impl<S1, S2> Stream for WhileBoth<S1, S2>
where
S1: Stream,
S2: Stream<Item = S1::Item, Error = S1::Error>,
{
type Item = S1::Item;
type Error = S1::Error;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
match self.0.poll() {
// Return errors or ready values (including the `None`
// that indicates the stream is empty) immediately.
r @ Err(_) | r @ Ok(Async::Ready(_)) => r,
// If the first stream is not ready, try the second one.
Ok(Async::NotReady) => self.1.poll(),
}
}
}
use futures::{Async,Poll,Stream};//0.1.25
结构WhileBoth(S1,S2)
哪里
S1:流,
S2:流;
WhileBoth的impl流
哪里
S1:流,
S2:溪流,
{
类型Item=S1::Item;
类型Error=S1::Error;
fn轮询(&mut self)->轮询{
匹配self.0.poll(){
//返回错误或就绪值(包括“无”`
//表示流是空的)立即。
r@Err(|)r@Ok(Async::Ready(|))=>r,
//如果第一个流未准备好,请尝试第二个流。
Ok(Async::NotReady)=>self.1.poll(),
}
}
}
另见:
use futures::{Async, Poll, Stream}; // 0.1.25
struct WhileBoth<S1, S2>(S1, S2)
where
S1: Stream,
S2: Stream<Item = S1::Item, Error = S1::Error>;
impl<S1, S2> Stream for WhileBoth<S1, S2>
where
S1: Stream,
S2: Stream<Item = S1::Item, Error = S1::Error>,
{
type Item = S1::Item;
type Error = S1::Error;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
match self.0.poll() {
// Return errors or ready values (including the `None`
// that indicates the stream is empty) immediately.
r @ Err(_) | r @ Ok(Async::Ready(_)) => r,
// If the first stream is not ready, try the second one.
Ok(Async::NotReady) => self.1.poll(),
}
}
}