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
use crate::body::{Body, HttpBody};
use crate::error::Error;

use futures::{Future, Poll};
use http::{Request, Response};
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 GrpcService<ReqBody> {
    /// Response body type
    type ResponseBody: Body + HttpBody;

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

    /// Error type
    type Error: Into<Error>;

    /// Poll that this service is ready.
    fn poll_ready(&mut self) -> Poll<(), Self::Error>;

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

    /// Helper when needing to pass this type to bounds needing `Service`.
    fn into_service(self) -> IntoService<Self>
    where
        Self: Sized,
    {
        IntoService(self)
    }

    /// Helper when needing to pass this type to bounds needing `Service`.
    fn as_service(&mut self) -> AsService<'_, Self>
    where
        Self: Sized,
    {
        AsService(self)
    }
}

impl<T, ReqBody, ResBody> GrpcService<ReqBody> for T
where
    T: Service<Request<ReqBody>, Response = Response<ResBody>>,
    T::Error: Into<Error>,
    ResBody: Body + HttpBody,
{
    type ResponseBody = ResBody;
    type Future = T::Future;
    type Error = T::Error;

    fn poll_ready(&mut self) -> Poll<(), Self::Error> {
        Service::poll_ready(self)
    }

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

/// Helper when needing to pass a `GrpcService` to bounds needing `Service`.
#[derive(Debug)]
pub struct AsService<'a, T>(&'a mut T);

impl<'a, T, ReqBody> Service<Request<ReqBody>> for AsService<'a, T>
where
    T: GrpcService<ReqBody> + 'a,
{
    type Response = Response<T::ResponseBody>;
    type Future = T::Future;
    type Error = T::Error;

    fn poll_ready(&mut self) -> Poll<(), Self::Error> {
        GrpcService::poll_ready(self.0)
    }

    fn call(&mut self, request: Request<ReqBody>) -> Self::Future {
        GrpcService::call(self.0, request)
    }
}

/// Helper when needing to pass a `GrpcService` to bounds needing `Service`.
#[derive(Debug)]
pub struct IntoService<T>(pub(crate) T);

impl<T, ReqBody> Service<Request<ReqBody>> for IntoService<T>
where
    T: GrpcService<ReqBody>,
{
    type Response = Response<T::ResponseBody>;
    type Future = T::Future;
    type Error = T::Error;

    fn poll_ready(&mut self) -> Poll<(), Self::Error> {
        GrpcService::poll_ready(&mut self.0)
    }

    fn call(&mut self, request: Request<ReqBody>) -> Self::Future {
        GrpcService::call(&mut self.0, request)
    }
}