logo
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
use crate::{FromRequest, IntoResponse, Request, Result};

use super::{FnExt, FnExtHandler, Handler};

/// Trait implemented by types that can be converted to a [`Handler`].
pub trait IntoHandler<E, I> {
    /// The target handler.
    type Handler: Handler<I>;

    /// Convert self to a [Handler].
    #[must_use]
    fn into_handler(self) -> Self::Handler;
}

impl<H, E, O> IntoHandler<E, Request> for H
where
    E: FromRequest + Send + Sync + 'static,
    E::Error: IntoResponse + Send + Sync,
    H: FnExt<E, Output = Result<O>>,
    O: Send + Sync + 'static,
{
    type Handler = FnExtHandler<H, E, O>;

    fn into_handler(self) -> Self::Handler {
        FnExtHandler::new(self)
    }
}