Trait gotham::handler::NewHandler

source ·
pub trait NewHandler: Send + Sync + RefUnwindSafe {
    type Instance: Handler + Send;

    // Required method
    fn new_handler(&self) -> Result<Self::Instance>;
}
Expand description

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) -> anyhow::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) -> anyhow::Result<Self::Instance> {
        Ok(MyHandler)
    }
}

struct MyHandler;

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

Required Associated Types§

source

type Instance: Handler + Send

The type of Handler created by the NewHandler.

Required Methods§

source

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

Create and return a new Handler value.

Implementations on Foreign Types§

source§

impl<H> NewHandler for Arc<H>
where H: NewHandler,

Implementors§