1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use std::marker::PhantomData;
use crate::{async_trait, FnExt, FromRequest, Handler, IntoResponse, Request, Result};
#[derive(Debug)]
pub struct FnExtHandler<H, E, O>(H, PhantomData<fn(E) -> O>);
impl<H, E, O> Clone for FnExtHandler<H, E, O>
where
H: Clone,
{
fn clone(&self) -> Self {
Self(self.0.clone(), PhantomData)
}
}
impl<H, E, O> FnExtHandler<H, E, O> {
pub(super) fn new(h: H) -> Self {
Self(h, PhantomData)
}
}
#[async_trait]
impl<H, E, O> Handler<Request> for FnExtHandler<H, E, O>
where
E: FromRequest + Send + Sync + 'static,
E::Error: IntoResponse + Send + Sync,
H: FnExt<E, Output = Result<O>>,
O: Send + Sync + 'static,
{
type Output = H::Output;
async fn call(&self, req: Request) -> Self::Output {
self.0
.call(req)
.await
.map_err(IntoResponse::into_error)
}
}