coap_handler

Trait Handler

Source
pub trait Handler {
    type Error<M: MinimalWritableMessage>: Debug + RenderableOnMinimal;

    // Required method
    async fn handle<R: Request>(
        &mut self,
        request: R,
    ) -> Result<<R as Request>::ResponseDone, Self::Error<<R as Request>::ResponseMessage>>;
}
Expand description

A CoAP request handler. This gets called by a CoAP server implementation that the handler is assigned to. The server processes the .handle() async method. As part of building a successful response, the handler takes the passed Request object, reads from it, turns it into a Response and writes into that response.

Response building is inherently fallible (the stack may not support some options), and other errors can just as well occur during extraction (for example checking for unprocessed critical options). Returning an error is not only an ergonomically easy step, it also allows the error writer to start over fresh: There is currently no trait method in coap_message that describes how that would be done, but the CoAP implementation knows the concrete type and can use its methods directly.

Required Associated Types§

Required Methods§

Source

async fn handle<R: Request>( &mut self, request: R, ) -> Result<<R as Request>::ResponseDone, Self::Error<<R as Request>::ResponseMessage>>

Processes an incoming request and build a response.

Implementations that do not have anything to await in their application logic still have at least one await point: the call to the async Request::response method.

CoAP servers may be implemented with limited buffer space. It is therefore recommended that long awaits (ideally, any awaits) are run between the call to Request::done() (at which point the CoAP server can start reusing the receive buffer) and the subsequent call to .respond() during which the server waits for a send buffer to be ready.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<H> Handler for Option<H>
where H: Handler,

An easy way to have resources that may or may not be there in a tree, considering that Handler is not object safe and thus, if let Some(x) { all = all.at(...) } won’t work.

This returns 4.04 Not Found if the inner handler is absent, and otherwise forwards request and response building.

Source§

type Error<M: MinimalWritableMessage> = Result<<H as Handler>::Error<M>, <M as MinimalWritableMessage>::UnionError>

Source§

async fn handle<R: Request>( &mut self, request: R, ) -> Result<<R as Request>::ResponseDone, Self::Error<<R as Request>::ResponseMessage>>

Implementors§