coap_server/app/
request_handler.rs

1use crate::app::{CoapError, Request, Response};
2use async_trait::async_trait;
3use dyn_clone::DynClone;
4use std::future::Future;
5
6#[async_trait]
7pub trait RequestHandler<Endpoint>: DynClone + 'static {
8    async fn handle(&self, request: Request<Endpoint>) -> Result<Response, CoapError>;
9}
10
11dyn_clone::clone_trait_object!(<Endpoint> RequestHandler<Endpoint>);
12
13#[async_trait]
14impl<Endpoint, F, R> RequestHandler<Endpoint> for F
15where
16    Endpoint: Send + Sync + 'static,
17    F: Fn(Request<Endpoint>) -> R + Sync + Send + Clone + 'static,
18    R: Future<Output = Result<Response, CoapError>> + Send,
19{
20    async fn handle(&self, request: Request<Endpoint>) -> Result<Response, CoapError> {
21        (self)(request).await
22    }
23}