tokio_proto/streaming/
body.rs

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
65
66
67
use std::fmt;

use futures::{Async, Poll, Stream};
use futures::sync::mpsc;

/// Body stream
pub struct Body<T, E> {
    inner: Inner<T, E>,
}

enum Inner<T, E> {
    Once(Option<T>),
    Stream(mpsc::Receiver<Result<T, E>>),
    Empty,
}

impl<T, E> Body<T, E> {
    /// Return an empty body stream
    pub fn empty() -> Body<T, E> {
        Body { inner: Inner::Empty }
    }

    /// Return a body stream with an associated sender half
    pub fn pair() -> (mpsc::Sender<Result<T, E>>, Body<T, E>) {
        let (tx, rx) = mpsc::channel(0);
        let rx = Body { inner: Inner::Stream(rx) };
        (tx, rx)
    }
}

impl<T, E> Stream for Body<T, E> {
    type Item = T;
    type Error = E;

    fn poll(&mut self) -> Poll<Option<T>, E> {
        match self.inner {
            Inner::Once(ref mut val) => Ok(Async::Ready(val.take())),
            Inner::Stream(ref mut s) => {
                match s.poll().unwrap() {
                    Async::Ready(None) => Ok(Async::Ready(None)),
                    Async::Ready(Some(Ok(e))) => Ok(Async::Ready(Some(e))),
                    Async::Ready(Some(Err(e))) => Err(e),
                    Async::NotReady => Ok(Async::NotReady),
                }
            }
            Inner::Empty => Ok(Async::Ready(None)),
        }
    }
}

impl<T, E> From<mpsc::Receiver<Result<T, E>>> for Body<T, E> {
    fn from(src: mpsc::Receiver<Result<T, E>>) -> Body<T, E> {
        Body { inner: Inner::Stream(src) }
    }
}

impl<T, E> From<T> for Body<T, E> {
    fn from(val: T) -> Body<T, E> {
        Body { inner: Inner::Once(Some(val)) }
    }
}

impl<T, E> fmt::Debug for Body<T, E> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "Body {{ [stream of values] }}")
    }
}