coap_server/
packet_handler.rs

1use std::pin::Pin;
2
3use coap_lite::Packet;
4use futures::Stream;
5
6/// "Low-level" raw packet handler intended to support the full range of CoAP features.  This
7/// is little more than a callback informing the user that a packet has arrived, allowing for
8/// an arbitrary number of arbitrary responses to be delivered back to this Endpoint.
9///
10/// Most customers should steer clear of this footgun.  It can be used in such a way that
11/// breaks protocol norms and could confuse clients easily.  Prefer [`crate::app::new`] instead.
12pub trait PacketHandler<Endpoint>: Clone {
13    fn handle<'a>(
14        &'a self,
15        packet: Packet,
16        peer: Endpoint,
17    ) -> Pin<Box<dyn Stream<Item = Packet> + Send + 'a>>;
18}
19
20pub trait IntoHandler<Handler, Endpoint>
21where
22    Handler: PacketHandler<Endpoint> + Send + 'static,
23{
24    fn into_handler(self, mtu: Option<u32>) -> Handler;
25}
26
27impl<Handler, Endpoint> IntoHandler<Handler, Endpoint> for Handler
28where
29    Handler: PacketHandler<Endpoint> + Send + 'static,
30{
31    fn into_handler(self, _mtu: Option<u32>) -> Handler {
32        self
33    }
34}