1use std::convert::Infallible;
2use std::future::Future;
3use std::pin::Pin;
4use std::sync::Arc;
5
6use hyper::body::{Bytes, Incoming};
7use hyper::{Request, Response};
8use http_body_util::combinators::BoxBody;
9use http_body_util::Full;
10
11use crate::error::ServeError;
12use crate::state::State;
13
14pub type ResponseBody = BoxBody<Bytes, Infallible>;
16
17pub fn body(bytes: Bytes) -> ResponseBody {
19 BoxBody::new(Full::new(bytes))
20}
21
22pub type Handler<S> = Arc<
27 dyn Fn(Request<Incoming>, State<S>)
28 -> Pin<Box<dyn Future<Output = Result<Response<ResponseBody>, ServeError>> + Send>>
29 + Send
30 + Sync,
31>;
32
33pub fn handler<S, F, Fut>(f: F) -> Handler<S>
49where
50 S: Send + Sync + 'static,
51 F: Fn(Request<Incoming>, State<S>) -> Fut + Send + Sync + 'static,
52 Fut: Future<Output = Result<Response<ResponseBody>, ServeError>> + Send + 'static,
53{
54 Arc::new(move |req, state| Box::pin(f(req, state)))
55}