Skip to main content

Handler

Trait Handler 

Source
pub trait Handler<T, S>:
    Clone
    + Send
    + Sized
    + 'static {
    type Future: Future<Output = Response> + Send + 'static;

    // Required method
    fn call(self, req: Request, state: S) -> Self::Future;
}
Expand description

A request handler: any async function whose arguments are extractors, and whose return type implements IntoResponse.

T is a marker type parameter used to disambiguate handler function arities/argument types via blanket impls (see the impl for () below).

Required Associated Types§

Source

type Future: Future<Output = Response> + Send + 'static

The future returned by Handler::call.

Required Methods§

Source

fn call(self, req: Request, state: S) -> Self::Future

Invoke the handler.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<F, Fut, R, S, A, B, C, D, E> Handler<(A, B, C, D, E), S> for F
where F: Fn(A, B, C, D, E) -> Fut + Clone + Send + 'static, Fut: Future<Output = R> + Send + 'static, R: IntoResponse, S: Clone + Send + 'static, A: FromRequestParts<S> + Send + 'static, B: FromRequestParts<S> + Send + 'static, C: FromRequestParts<S> + Send + 'static, D: FromRequestParts<S> + Send + 'static, E: FromRequest<S, BoxBody> + Send + 'static,

5-argument handler: fn(A, B, C, D, E) -> Fut.

A, B, C, D are extracted via FromRequestParts; E is extracted via FromRequest.

Source§

type Future = Pin<Box<dyn Future<Output = Response<BoxBody<Bytes, Box<dyn Error + Send + Sync>>>> + Send>>

Source§

impl<F, Fut, R, S, A, B, C, D> Handler<(A, B, C, D), S> for F
where F: Fn(A, B, C, D) -> Fut + Clone + Send + 'static, Fut: Future<Output = R> + Send + 'static, R: IntoResponse, S: Clone + Send + 'static, A: FromRequestParts<S> + Send + 'static, B: FromRequestParts<S> + Send + 'static, C: FromRequestParts<S> + Send + 'static, D: FromRequest<S, BoxBody> + Send + 'static,

4-argument handler: fn(A, B, C, D) -> Fut.

A, B, and C are extracted via FromRequestParts; D is extracted via FromRequest.

Source§

type Future = Pin<Box<dyn Future<Output = Response<BoxBody<Bytes, Box<dyn Error + Send + Sync>>>> + Send>>

Source§

impl<F, Fut, R, S, A, B, C> Handler<(A, B, C), S> for F
where F: Fn(A, B, C) -> Fut + Clone + Send + 'static, Fut: Future<Output = R> + Send + 'static, R: IntoResponse, S: Clone + Send + 'static, A: FromRequestParts<S> + Send + 'static, B: FromRequestParts<S> + Send + 'static, C: FromRequest<S, BoxBody> + Send + 'static,

3-argument handler: fn(A, B, C) -> Fut.

A and B are extracted via FromRequestParts; C is extracted via FromRequest. If any of A or B fails, the body is never touched.

Source§

type Future = Pin<Box<dyn Future<Output = Response<BoxBody<Bytes, Box<dyn Error + Send + Sync>>>> + Send>>

Source§

impl<F, Fut, R, S, A, B> Handler<(A, B), S> for F
where F: Fn(A, B) -> Fut + Clone + Send + 'static, Fut: Future<Output = R> + Send + 'static, R: IntoResponse, S: Clone + Send + 'static, A: FromRequestParts<S> + Send + 'static, B: FromRequest<S, BoxBody> + Send + 'static,

2-argument handler: fn(A, B) -> Fut.

A is extracted via FromRequestParts, B is extracted via FromRequest. If A fails, the body is never touched.

Source§

type Future = Pin<Box<dyn Future<Output = Response<BoxBody<Bytes, Box<dyn Error + Send + Sync>>>> + Send>>

Source§

impl<F, Fut, R, S, E> Handler<(E,), S> for F
where F: Fn(E) -> Fut + Clone + Send + 'static, Fut: Future<Output = R> + Send + 'static, R: IntoResponse, S: Clone + Send + 'static, E: FromRequest<S, BoxBody> + Send + 'static,

Blanket impl for handlers that take a single extractor argument.

Any async fn F: Fn(E) -> Fut where Fut: Future<Output = R>, R: IntoResponse, and E: FromRequest<S, BoxBody> is a valid handler.

The argument E is extracted via FromRequest::from_request, which gets the full request (including body). For parts-only extractors the blanket in extract.rs turns this into a simple parts extraction, dropping the body. For body-consuming extractors (like [Json<T>]) the body is read and deserialized as normal.

This single blanket replaces what were previously separate impls for FromRequestParts (marker (E,)) and FromRequest (marker (E, BoxBody)), because every FromRequestParts type now also implements FromRequest via the blanket in extract.rs.

Source§

type Future = Pin<Box<dyn Future<Output = Response<BoxBody<Bytes, Box<dyn Error + Send + Sync>>>> + Send>>

Source§

impl<F, Fut, R, S> Handler<(), S> for F
where F: Fn() -> Fut + Clone + Send + 'static, Fut: Future<Output = R> + Send + 'static, R: IntoResponse,

Blanket impl for closures / functions that take zero extractors.

Any async fn F: Fn() -> Fut where Fut: Future<Output = R> and R: IntoResponse is a valid zero-argument handler.

Source§

type Future = Pin<Box<dyn Future<Output = Response<BoxBody<Bytes, Box<dyn Error + Send + Sync>>>> + Send>>