use std::rc::Rc;
#[allow(async_fn_in_trait)]
pub trait Service<R> {
type Response;
type Error;
async fn call(&self, request: R) -> Result<Self::Response, Self::Error>;
}
#[inline]
pub fn service_fn<F>(function: F) -> ServiceFn<F> {
ServiceFn { function }
}
#[derive(Clone, Copy, Debug)]
pub struct ServiceFn<F> {
function: F,
}
impl<R, F, T, E> Service<R> for ServiceFn<F>
where
F: AsyncFn(R) -> Result<T, E>,
{
type Response = T;
type Error = E;
#[inline]
async fn call(&self, request: R) -> Result<T, E> {
(self.function)(request).await
}
}
impl<R, S: Service<R> + ?Sized> Service<R> for &S {
type Response = S::Response;
type Error = S::Error;
#[inline]
async fn call(&self, request: R) -> Result<Self::Response, Self::Error> {
(**self).call(request).await
}
}
impl<R, S: Service<R> + ?Sized> Service<R> for Box<S> {
type Response = S::Response;
type Error = S::Error;
#[inline]
async fn call(&self, request: R) -> Result<Self::Response, Self::Error> {
(**self).call(request).await
}
}
impl<R, S: Service<R> + ?Sized> Service<R> for Rc<S> {
type Response = S::Response;
type Error = S::Error;
#[inline]
async fn call(&self, request: R) -> Result<Self::Response, Self::Error> {
(**self).call(request).await
}
}