tokio_buf/util/
chain.rs

1use BufStream;
2
3use either::Either;
4use futures::Poll;
5
6/// A buf stream that sequences two buf streams together.
7///
8/// `Chain` values are produced by the `chain` function on `BufStream`.
9#[derive(Debug)]
10pub struct Chain<T, U> {
11    left: Option<T>,
12    right: U,
13}
14
15impl<T, U> Chain<T, U> {
16    pub(crate) fn new(left: T, right: U) -> Chain<T, U> {
17        Chain {
18            left: Some(left),
19            right,
20        }
21    }
22}
23
24impl<T, U> BufStream for Chain<T, U>
25where
26    T: BufStream,
27    U: BufStream<Error = T::Error>,
28{
29    type Item = Either<T::Item, U::Item>;
30    type Error = T::Error;
31
32    fn poll_buf(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
33        if let Some(ref mut stream) = self.left {
34            let res = try_ready!(stream.poll_buf());
35
36            if res.is_some() {
37                return Ok(res.map(Either::Left).into());
38            }
39        }
40
41        self.left = None;
42
43        let res = try_ready!(self.right.poll_buf());
44        Ok(res.map(Either::Right).into())
45    }
46}