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
use crate::codec::{Encode, Encoder, Streaming};
use crate::generic::server::{server_streaming, ServerStreamingService};
use crate::Body;

use futures::{try_ready, Future, Poll};
use std::fmt;

pub struct ResponseFuture<T, B, R>
where
    T: ServerStreamingService<R>,
    B: Body,
    R: prost::Message + Default,
{
    inner: Inner<T, T::Response, R, B>,
}

type Inner<T, U, V, B> = server_streaming::ResponseFuture<T, Encoder<U>, Streaming<V, B>>;

impl<T, B, R> ResponseFuture<T, B, R>
where
    T: ServerStreamingService<R>,
    R: prost::Message + Default,
    T::Response: prost::Message,
    B: Body,
{
    pub(crate) fn new(inner: Inner<T, T::Response, R, B>) -> Self {
        ResponseFuture { inner }
    }
}

impl<T, B, R> Future for ResponseFuture<T, B, R>
where
    T: ServerStreamingService<R>,
    R: prost::Message + Default,
    T::Response: prost::Message,
    B: Body,
{
    type Item = http::Response<Encode<T::ResponseStream>>;
    type Error = crate::error::Never;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        let response = try_ready!(self.inner.poll());
        let response = response.map(Encode::new);
        Ok(response.into())
    }
}

impl<T, B, R> fmt::Debug for ResponseFuture<T, B, R>
where
    T: ServerStreamingService<R> + fmt::Debug,
    T::Response: fmt::Debug,
    T::ResponseStream: fmt::Debug,
    T::Future: fmt::Debug,
    B: Body + fmt::Debug,
    B::Data: fmt::Debug,
    R: prost::Message + Default,
{
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct("server_streaming::ResponseFuture")
            .field("inner", &self.inner)
            .finish()
    }
}