1 use crate::{Request, Response, Status, Streaming}; 2 use std::future::Future; 3 use tokio_stream::Stream; 4 use 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`. 10 pub 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 call(&mut self, request: Request<R>) -> Self::Future18 fn call(&mut self, request: Request<R>) -> Self::Future; 19 } 20 21 impl<T, M1, M2> UnaryService<M1> for T 22 where 23 T: Service<Request<M1>, Response = Response<M2>, Error = crate::Status>, 24 { 25 type Response = M2; 26 type Future = T::Future; 27 call(&mut self, request: Request<M1>) -> Self::Future28 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`. 37 pub 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 call(&mut self, request: Request<R>) -> Self::Future48 fn call(&mut self, request: Request<R>) -> Self::Future; 49 } 50 51 impl<T, S, M1, M2> ServerStreamingService<M1> for T 52 where 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 call(&mut self, request: Request<M1>) -> Self::Future60 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`. 69 pub 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 call(&mut self, request: Request<Streaming<R>>) -> Self::Future77 fn call(&mut self, request: Request<Streaming<R>>) -> Self::Future; 78 } 79 80 impl<T, M1, M2> ClientStreamingService<M1> for T 81 where 82 T: Service<Request<Streaming<M1>>, Response = Response<M2>, Error = crate::Status>, 83 { 84 type Response = M2; 85 type Future = T::Future; 86 call(&mut self, request: Request<Streaming<M1>>) -> Self::Future87 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`. 96 pub 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 call(&mut self, request: Request<Streaming<R>>) -> Self::Future107 fn call(&mut self, request: Request<Streaming<R>>) -> Self::Future; 108 } 109 110 impl<T, S, M1, M2> StreamingService<M1> for T 111 where 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 call(&mut self, request: Request<Streaming<M1>>) -> Self::Future119 fn call(&mut self, request: Request<Streaming<M1>>) -> Self::Future { 120 Service::call(self, request) 121 } 122 } 123