Skip to main content

Handler

Trait Handler 

Source
pub trait Handler<I, E: Render = NoRender>:
    Send
    + Sync
    + 'static {
    // Required method
    fn call(&self, cx: &mut Context<'_, I, E>) -> Result;
}
Expand description

A request handler. Any Fn(&mut Context<'_, I, E>) -> web_net::Result is one.

use serve_net::Method;
use web_net::{App, Context, Handler, Render, Result, Route};

// A handler that carries its own data.
struct Greeting(&'static str);
impl<I, E: Render> Handler<I, E> for Greeting {
    fn call(&self, cx: &mut Context<'_, I, E>) -> Result {
        cx.text(200, self.0)
    }
}

let app = App::builder().route(Route::at("/").on(Method::Get, Greeting("hello"))).build()?;
let r = serve_net::testing::respond(&app, b"GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
assert_eq!(r.text(), "hello");

Required Methods§

Source

fn call(&self, cx: &mut Context<'_, I, E>) -> Result

Handles one cleared request.

§Errors

An Error, answered with the site’s error page for its status.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§

Source§

impl<I, E: Render, F> Handler<I, E> for F
where F: Fn(&mut Context<'_, I, E>) -> Result + Send + Sync + 'static,