use crate::http::{Request, Response};
use crate::stream::Stream;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
pub trait WebsocketHandler<State>: Send + Sync {
#[allow(missing_docs)]
fn serve(
&self,
request: Request,
stream: Stream,
state: Arc<State>,
) -> Pin<Box<dyn Future<Output = ()> + Send>>;
}
impl<F, Fut, State> WebsocketHandler<State> for F
where
F: Fn(Request, Stream, Arc<State>) -> Fut + Send + Sync,
Fut: Future<Output = ()> + Send + 'static,
{
fn serve(
&self,
request: Request,
stream: Stream,
state: Arc<State>,
) -> Pin<Box<dyn Future<Output = ()> + Send>> {
Box::pin(self(request, stream, state))
}
}
pub trait RequestHandler<State>: Send + Sync {
#[allow(missing_docs)]
fn serve(
&self,
request: Request,
state: Arc<State>,
) -> Pin<Box<dyn Future<Output = Response> + Send>>;
}
impl<F, Fut, State> RequestHandler<State> for F
where
F: Fn(Request, Arc<State>) -> Fut + Send + Sync,
Fut: Future<Output = Response> + Send + 'static,
{
fn serve(
&self,
request: Request,
state: Arc<State>,
) -> Pin<Box<dyn Future<Output = Response> + Send>> {
Box::pin(self(request, state))
}
}
pub trait StatelessRequestHandler<State>: Send + Sync {
#[allow(missing_docs)]
fn serve(&self, request: Request) -> Pin<Box<dyn Future<Output = Response> + Send>>;
}
impl<F, Fut, State> StatelessRequestHandler<State> for F
where
F: Fn(Request) -> Fut + Send + Sync,
Fut: Future<Output = Response> + Send + 'static,
{
fn serve(&self, request: Request) -> Pin<Box<dyn Future<Output = Response> + Send>> {
Box::pin(self(request))
}
}
pub trait PathAwareRequestHandler<State>: Send + Sync {
#[allow(missing_docs)]
fn serve(
&self,
request: Request,
state: Arc<State>,
route: &'static str,
) -> Pin<Box<dyn Future<Output = Response> + Send>>;
}
impl<F, Fut, State> PathAwareRequestHandler<State> for F
where
F: Fn(Request, Arc<State>, &'static str) -> Fut + Send + Sync,
Fut: Future<Output = Response> + Send + 'static,
{
fn serve(
&self,
request: Request,
state: Arc<State>,
route: &'static str,
) -> Pin<Box<dyn Future<Output = Response> + Send>> {
Box::pin(self(request, state, route))
}
}