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
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
mod grpc;

pub(crate) mod client_streaming;
pub(crate) mod server_streaming;
pub(crate) mod streaming;
pub(crate) mod unary;

pub(crate) use self::grpc::Grpc;

use crate::{Request, Response};

use futures::{Future, Stream};
use tower_service::Service;

/// A specialization of tower_service::Service.
///
/// Existing tower_service::Service implementations with the correct form will
/// automatically implement `GrpcService`.
pub trait StreamingService<RequestStream> {
    /// Protobuf response message type
    type Response;

    /// Stream of outbound response messages
    type ResponseStream: Stream<Item = Self::Response, Error = crate::Status>;

    /// Response future
    type Future: Future<Item = crate::Response<Self::ResponseStream>, Error = crate::Status>;

    /// Call the service
    fn call(&mut self, request: Request<RequestStream>) -> Self::Future;
}

impl<T, S1, S2> StreamingService<S1> for T
where
    T: Service<Request<S1>, Response = Response<S2>, Error = crate::Status>,
    S1: Stream<Error = crate::Status>,
    S2: Stream<Error = crate::Status>,
{
    type Response = S2::Item;
    type ResponseStream = S2;
    type Future = T::Future;

    fn call(&mut self, request: Request<S1>) -> Self::Future {
        Service::call(self, request)
    }
}

/// A specialization of tower_service::Service.
///
/// Existing tower_service::Service implementations with the correct form will
/// automatically implement `UnaryService`.
pub trait UnaryService<R> {
    /// Protobuf response message type
    type Response;

    /// Response future
    type Future: Future<Item = crate::Response<Self::Response>, Error = crate::Status>;

    /// Call the service
    fn call(&mut self, request: Request<R>) -> Self::Future;
}

impl<T, M1, M2> UnaryService<M1> for T
where
    T: Service<Request<M1>, Response = Response<M2>, Error = crate::Status>,
{
    type Response = M2;
    type Future = T::Future;

    fn call(&mut self, request: Request<M1>) -> Self::Future {
        Service::call(self, request)
    }
}

/// A specialization of tower_service::Service.
///
/// Existing tower_service::Service implementations with the correct form will
/// automatically implement `UnaryService`.
pub trait ClientStreamingService<RequestStream> {
    /// Protobuf response message type
    type Response;

    /// Response future
    type Future: Future<Item = crate::Response<Self::Response>, Error = crate::Status>;

    /// Call the service
    fn call(&mut self, request: Request<RequestStream>) -> Self::Future;
}

impl<T, M, S> ClientStreamingService<S> for T
where
    T: Service<Request<S>, Response = Response<M>, Error = crate::Status>,
    S: Stream<Error = crate::Status>,
{
    type Response = M;
    type Future = T::Future;

    fn call(&mut self, request: Request<S>) -> Self::Future {
        Service::call(self, request)
    }
}

/// A specialization of tower_service::Service.
///
/// Existing tower_service::Service implementations with the correct form will
/// automatically implement `UnaryService`.
pub trait ServerStreamingService<R> {
    /// Protobuf response message type
    type Response;

    /// Stream of outbound response messages
    type ResponseStream: Stream<Item = Self::Response, Error = crate::Status>;

    /// Response future
    type Future: Future<Item = crate::Response<Self::ResponseStream>, Error = crate::Status>;

    /// Call the service
    fn call(&mut self, request: Request<R>) -> Self::Future;
}

impl<T, M, S> ServerStreamingService<M> for T
where
    T: Service<Request<M>, Response = Response<S>, Error = crate::Status>,
    S: Stream<Error = crate::Status>,
{
    type Response = S::Item;
    type ResponseStream = S;
    type Future = T::Future;

    fn call(&mut self, request: Request<M>) -> Self::Future {
        Service::call(self, request)
    }
}