Handler

Trait Handler 

Source
pub trait Handler:
    Clone
    + Send
    + Sync
    + 'static {
    type Future: Future<Output = Result<HttpResponse, Error>> + Send + 'static;

    // Required method
    fn call(&self, req: HttpRequest) -> Self::Future;
}
Expand description

A handler that can process HTTP requests.

This trait is designed to be monomorphized: each implementation gets its own specialized code path, allowing the compiler to inline the handler body.

§Example

async fn my_handler(req: HttpRequest) -> Result<HttpResponse, Error> {
    Ok(HttpResponse::ok())
}

// The handler is automatically converted via IntoHandler
let handler = my_handler.into_handler();

Required Associated Types§

Source

type Future: Future<Output = Result<HttpResponse, Error>> + Send + 'static

The future returned by call.

Using an associated type instead of Box<dyn Future> allows the compiler to monomorphize this future type, enabling inlining.

Required Methods§

Source

fn call(&self, req: HttpRequest) -> Self::Future

Handle an HTTP request.

This method should be inlined by the compiler due to monomorphization. Note: #[inline] hints are applied in implementations, not trait definitions.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<F, Fut> Handler for FnHandler<F>
where F: Fn(HttpRequest) -> Fut + Clone + Send + Sync + 'static, Fut: Future<Output = Result<HttpResponse, Error>> + Send + 'static,

Source§

type Future = Fut