tower_grpc/generic/server/
mod.rs

1mod grpc;
2
3pub(crate) mod client_streaming;
4pub(crate) mod server_streaming;
5pub(crate) mod streaming;
6pub(crate) mod unary;
7
8pub(crate) use self::grpc::Grpc;
9
10use crate::{Request, Response};
11
12use futures::{Future, Stream};
13use tower_service::Service;
14
15/// A specialization of tower_service::Service.
16///
17/// Existing tower_service::Service implementations with the correct form will
18/// automatically implement `GrpcService`.
19pub trait StreamingService<RequestStream> {
20    /// Protobuf response message type
21    type Response;
22
23    /// Stream of outbound response messages
24    type ResponseStream: Stream<Item = Self::Response, Error = crate::Status>;
25
26    /// Response future
27    type Future: Future<Item = crate::Response<Self::ResponseStream>, Error = crate::Status>;
28
29    /// Call the service
30    fn call(&mut self, request: Request<RequestStream>) -> Self::Future;
31}
32
33impl<T, S1, S2> StreamingService<S1> for T
34where
35    T: Service<Request<S1>, Response = Response<S2>, Error = crate::Status>,
36    S1: Stream<Error = crate::Status>,
37    S2: Stream<Error = crate::Status>,
38{
39    type Response = S2::Item;
40    type ResponseStream = S2;
41    type Future = T::Future;
42
43    fn call(&mut self, request: Request<S1>) -> Self::Future {
44        Service::call(self, request)
45    }
46}
47
48/// A specialization of tower_service::Service.
49///
50/// Existing tower_service::Service implementations with the correct form will
51/// automatically implement `UnaryService`.
52pub trait UnaryService<R> {
53    /// Protobuf response message type
54    type Response;
55
56    /// Response future
57    type Future: Future<Item = crate::Response<Self::Response>, Error = crate::Status>;
58
59    /// Call the service
60    fn call(&mut self, request: Request<R>) -> Self::Future;
61}
62
63impl<T, M1, M2> UnaryService<M1> for T
64where
65    T: Service<Request<M1>, Response = Response<M2>, Error = crate::Status>,
66{
67    type Response = M2;
68    type Future = T::Future;
69
70    fn call(&mut self, request: Request<M1>) -> Self::Future {
71        Service::call(self, request)
72    }
73}
74
75/// A specialization of tower_service::Service.
76///
77/// Existing tower_service::Service implementations with the correct form will
78/// automatically implement `UnaryService`.
79pub trait ClientStreamingService<RequestStream> {
80    /// Protobuf response message type
81    type Response;
82
83    /// Response future
84    type Future: Future<Item = crate::Response<Self::Response>, Error = crate::Status>;
85
86    /// Call the service
87    fn call(&mut self, request: Request<RequestStream>) -> Self::Future;
88}
89
90impl<T, M, S> ClientStreamingService<S> for T
91where
92    T: Service<Request<S>, Response = Response<M>, Error = crate::Status>,
93    S: Stream<Error = crate::Status>,
94{
95    type Response = M;
96    type Future = T::Future;
97
98    fn call(&mut self, request: Request<S>) -> Self::Future {
99        Service::call(self, request)
100    }
101}
102
103/// A specialization of tower_service::Service.
104///
105/// Existing tower_service::Service implementations with the correct form will
106/// automatically implement `UnaryService`.
107pub trait ServerStreamingService<R> {
108    /// Protobuf response message type
109    type Response;
110
111    /// Stream of outbound response messages
112    type ResponseStream: Stream<Item = Self::Response, Error = crate::Status>;
113
114    /// Response future
115    type Future: Future<Item = crate::Response<Self::ResponseStream>, Error = crate::Status>;
116
117    /// Call the service
118    fn call(&mut self, request: Request<R>) -> Self::Future;
119}
120
121impl<T, M, S> ServerStreamingService<M> for T
122where
123    T: Service<Request<M>, Response = Response<S>, Error = crate::Status>,
124    S: Stream<Error = crate::Status>,
125{
126    type Response = S::Item;
127    type ResponseStream = S;
128    type Future = T::Future;
129
130    fn call(&mut self, request: Request<M>) -> Self::Future {
131        Service::call(self, request)
132    }
133}