1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::marker::PhantomData;
use async_codec::{AsyncDecode, DecodeError};
use futures_core::Poll;
use futures_core::Async::Ready;
use futures_core::task::Context;
use futures_io::AsyncRead;
pub struct Chain<R, S, T>
where R: AsyncRead,
S: AsyncDecode<R>
{
first: S,
second: T,
first_item: Option<S::Item>,
_r: PhantomData<R>,
}
impl<R, S, T> Chain<R, S, T>
where R: AsyncRead,
S: AsyncDecode<R>
{
pub fn new(first: S, second: T) -> Chain<R, S, T> {
Chain {
first,
second,
first_item: None,
_r: PhantomData,
}
}
}
impl<R, S, T> AsyncDecode<R> for Chain<R, S, T>
where R: AsyncRead,
S: AsyncDecode<R>,
T: AsyncDecode<R, Error = S::Error>
{
type Item = (S::Item, T::Item);
type Error = S::Error;
fn poll_decode(&mut self,
cx: &mut Context,
reader: &mut R)
-> Poll<(Option<Self::Item>, usize), DecodeError<Self::Error>> {
if self.first_item.is_none() {
match try_ready!(self.first.poll_decode(cx, reader)) {
(None, read) => Ok(Ready((None, read))),
(Some(item), read) => {
self.first_item = Some(item);
Ok(Ready((None, read)))
}
}
} else {
match try_ready!(self.second.poll_decode(cx, reader)) {
(None, read) => Ok(Ready((None, read))),
(Some(item), read) => {
Ok(Ready((Some((self.first_item.take().unwrap(), item)), read)))
}
}
}
}
}