futures_util/stream/
chain.rsuse core::mem;
use futures_core::{Stream, Async, Poll};
use futures_core::task;
#[derive(Debug)]
enum State<S1, S2> {
First(S1, S2),
Second(S2),
Temp,
}
#[derive(Debug)]
#[must_use = "streams do nothing unless polled"]
pub struct Chain<S1, S2> {
state: State<S1, S2>
}
pub fn new<S1, S2>(s1: S1, s2: S2) -> Chain<S1, S2>
where S1: Stream, S2: Stream<Item=S1::Item, Error=S1::Error>,
{
Chain { state: State::First(s1, s2) }
}
impl<S1, S2> Stream for Chain<S1, S2>
where S1: Stream, S2: Stream<Item=S1::Item, Error=S1::Error>,
{
type Item = S1::Item;
type Error = S1::Error;
fn poll_next(&mut self, cx: &mut task::Context) -> Poll<Option<Self::Item>, Self::Error> {
loop {
match self.state {
State::First(ref mut s1, ref _s2) => match s1.poll_next(cx) {
Ok(Async::Ready(None)) => (), x => return x,
},
State::Second(ref mut s2) => return s2.poll_next(cx),
State::Temp => unreachable!(),
}
self.state = match mem::replace(&mut self.state, State::Temp) {
State::First(_s1, s2) => State::Second(s2),
_ => unreachable!(),
};
}
}
}