[][src]Trait gotham::handler::NewHandler

pub trait NewHandler: Send + Sync + RefUnwindSafe {
    type Instance: Handler + Send;
    fn new_handler(&self) -> Result<Self::Instance>;
}

A type which is used to spawn new Handler values. When implementing a custom Handler type, this is used to define how instances of the Handler are created.

The Instance associated type is usually Self in the simple case, but can be a different type where greater control is needed over lifetimes.

Examples

A custom handler which implements NewHandler by copying itself.

#[derive(Copy, Clone)]
struct MyCustomHandler;

impl NewHandler for MyCustomHandler {
    type Instance = Self;

    fn new_handler(&self) -> Result<Self::Instance> {
        Ok(*self)
    }
}

impl Handler for MyCustomHandler {
    fn handle(self, _state: State) -> Pin<Box<HandlerFuture>> {
        // Implementation elided.
    }
}

A custom handler which implements NewHandler using a specific Instance type.

#[derive(Copy, Clone)]
struct MyValueInstantiatingHandler;

impl NewHandler for MyValueInstantiatingHandler {
    type Instance = MyHandler;

    fn new_handler(&self) -> Result<Self::Instance> {
        Ok(MyHandler)
    }
}

struct MyHandler;

impl Handler for MyHandler {
    fn handle(self, _state: State) -> Pin<Box<HandlerFuture>> {
        // Implementation elided.
    }
}

Associated Types

type Instance: Handler + Send

The type of Handler created by the NewHandler.

Loading content...

Required methods

fn new_handler(&self) -> Result<Self::Instance>

Create and return a new Handler value.

Loading content...

Implementors

impl NewHandler for DirHandler[src]

type Instance = Self

impl NewHandler for FileHandler[src]

type Instance = Self

impl NewHandler for Router[src]

type Instance = Router

impl<F, H> NewHandler for F where
    F: Fn() -> Result<H> + Send + Sync + RefUnwindSafe,
    H: Handler + Send
[src]

type Instance = H

Loading content...