Type Definition aitch::BoxedHandler[][src]

type BoxedHandler = Box<Handler<BodyStream, Resp = BoxedResponse>>;

A Box<Handler> with all types erased.

This allows multiple handlers with different type signature (e.g. different body/responder types) to be stored in the same data-structure.

Example

/// A Very Simple Router which matches handlers based on the full path in request URIs.
struct VerySimpleRouter(pub HashMap<String, BoxedHandler>);

impl Handler<BodyStream> for VerySimpleRouter {
    type Resp = BoxedResponse;

    fn handle(&self, req: Request<BodyStream>, mut resp: ResponseBuilder) -> BoxedResponse {
        match self.0.get(req.uri().path()) {
            Some(handler) => handler.handle(req, resp),
            // Note the use of `.into_response()`, which boxes the response to erase any
            // type variables used by the responder, matching the response type used by
            // `BoxedHandler` above.
            None => resp.status(http::StatusCode::NOT_FOUND).body(()).into_response(),
        }
    }
}

See the SimpleRouter source code for a (slightly) more complete example.