pub trait NewHandler: Send + Sync + RefUnwindSafe {
    type Instance: Handler + Send;
    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) -> Result<Self::Instance> {
        Ok(*self)
    }
}

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

Associated Types

The type of Handler created by the NewHandler.

Required methods

Create and return a new Handler value.

Implementors