vidi_tower/
service.rs

1use std::{
2    future::Future,
3    pin::Pin,
4    task::{Context, Poll},
5};
6
7use vidi_core::{Error, Handler, Request, Response, Result};
8
9/// An adapter that makes a [`Handler`] into a [`Service`](tower::Service).
10#[derive(Debug)]
11pub struct HandlerService<H>(H);
12
13impl<H> HandlerService<H> {
14    /// Creates a new [`HandlerService`].
15    pub const fn new(h: H) -> Self {
16        Self(h)
17    }
18}
19
20impl<H> Clone for HandlerService<H>
21where
22    H: Clone,
23{
24    fn clone(&self) -> Self {
25        Self(self.0.clone())
26    }
27}
28
29impl<H> tower::Service<Request> for HandlerService<H>
30where
31    H: Handler<Request, Output = Result<Response>> + Clone,
32{
33    type Response = Response;
34    type Error = Error;
35    type Future = Pin<Box<dyn Future<Output = H::Output> + Send>>;
36
37    #[inline]
38    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
39        Poll::Ready(Ok(()))
40    }
41
42    fn call(&mut self, req: Request) -> Self::Future {
43        let h = self.0.clone();
44        Box::pin(async move { h.call(req).await })
45    }
46}