Skip to main content

Handler

Trait Handler 

Source
pub trait Handler:
    Send
    + Sync
    + 'static {
    // Required method
    fn handle(&self, call: Call) -> HandlerFuture;
}
Expand description

Anything that can handle a Call and produce a Response.

This is the type-erased shape the Router stores (as a BoxHandler). You rarely implement it by hand — async closures become Handlers through IntoHandler — but you can implement it directly for a type that needs to carry state.

use churust_core::{Call, Handler, Response, IntoResponse, handler::HandlerFuture};
use http::{HeaderMap, Method};
use bytes::Bytes;

struct Greeter;
impl Handler for Greeter {
    fn handle(&self, _call: Call) -> HandlerFuture {
        Box::pin(async { "hi".into_response() })
    }
}
let call = Call::new(Method::GET, "/".parse().unwrap(), HeaderMap::new(), Bytes::new());
let res = Greeter.handle(call).await;
assert_eq!(res.body.as_slice(), Some(&b"hi"[..]));

Required Methods§

Source

fn handle(&self, call: Call) -> HandlerFuture

Handle the call, returning a future that resolves to the response.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§

Source§

impl Handler for BoxHandler

Arc<dyn Handler> (i.e. BoxHandler) is itself a Handler, so a pre-boxed handler can be re-boxed or passed to the router builders.

Source§

impl<Marker, H> Handler for HandlerFnAdapter<Marker, H>
where Marker: 'static, H: HandlerFn<Marker>,