tonic/server/
service.rs

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