use crate::request::Request;
use crate::response::{IntoResponse, Response};
use std::future::Future;
use std::pin::Pin;
pub type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send + 'static>>;
pub trait Handler: Send + Sync + 'static {
fn call(&self, request: Request) -> BoxFuture<Response>;
}
impl<F, Fut, R> Handler for F
where
F: Fn(Request) -> Fut + Send + Sync + 'static,
Fut: Future<Output = R> + Send + 'static,
R: IntoResponse + 'static,
{
fn call(&self, request: Request) -> BoxFuture<Response> {
let future = self(request);
Box::pin(async move { future.await.into_response() })
}
}
pub struct Fixed(pub Response);
impl Handler for Fixed {
fn call(&self, _request: Request) -> BoxFuture<Response> {
let response = self.0.clone();
Box::pin(async move { response })
}
}