1 use http_body::Body;
2 use std::future::Future;
3 use std::task::{Context, Poll};
4 use tower_service::Service;
5 
6 /// Definition of the gRPC trait alias for [`tower_service`].
7 ///
8 /// This trait enforces that all tower services provided to [`Grpc`] implements
9 /// the correct traits.
10 ///
11 /// [`Grpc`]: ../client/struct.Grpc.html
12 /// [`tower_service`]: https://docs.rs/tower-service
13 pub trait GrpcService<ReqBody> {
14     /// Responses body given by the service.
15     type ResponseBody: Body;
16     /// Errors produced by the service.
17     type Error: Into<crate::Error>;
18     /// The future response value.
19     type Future: Future<Output = Result<http::Response<Self::ResponseBody>, Self::Error>>;
20 
21     /// Returns `Ready` when the service is able to process requests.
22     ///
23     /// Reference [`Service::poll_ready`].
poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>24     fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>;
25 
26     /// Process the request and return the response asynchronously.
27     ///
28     /// Reference [`Service::call`].
call(&mut self, request: http::Request<ReqBody>) -> Self::Future29     fn call(&mut self, request: http::Request<ReqBody>) -> Self::Future;
30 }
31 
32 impl<T, ReqBody, ResBody> GrpcService<ReqBody> for T
33 where
34     T: Service<http::Request<ReqBody>, Response = http::Response<ResBody>>,
35     T::Error: Into<crate::Error>,
36     ResBody: Body,
37     <ResBody as Body>::Error: Into<crate::Error>,
38 {
39     type ResponseBody = ResBody;
40     type Error = T::Error;
41     type Future = T::Future;
42 
poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>43     fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
44         Service::poll_ready(self, cx)
45     }
46 
call(&mut self, request: http::Request<ReqBody>) -> Self::Future47     fn call(&mut self, request: http::Request<ReqBody>) -> Self::Future {
48         Service::call(self, request)
49     }
50 }
51