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