Skip to main content

yaiko_core/
handler.rs

1use crate::{Request, Response};
2use async_trait::async_trait;
3use std::future::Future;
4use std::pin::Pin;
5
6pub type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send + 'static>>;
7
8#[async_trait]
9pub trait Handler: Send + Sync + 'static {
10    async fn handle(
11        &self,
12        req: Request,
13    ) -> Result<Response, Box<dyn std::error::Error + Send + Sync>>;
14}
15
16#[async_trait]
17impl<F, Fut> Handler for F
18where
19    F: Fn(Request) -> Fut + Send + Sync + 'static,
20    Fut: Future<Output = Result<Response, Box<dyn std::error::Error + Send + Sync>>>
21        + Send
22        + 'static,
23{
24    async fn handle(
25        &self,
26        req: Request,
27    ) -> Result<Response, Box<dyn std::error::Error + Send + Sync>> {
28        self(req).await
29    }
30}