Skip to main content

dnet_utils/
unwrap.rs

1//! Unwrapping incoming messages.
2
3#![allow(clippy::type_complexity)]
4
5#[cfg(feature = "logging")]
6use dnet_base::Logging;
7
8use super::map::{Map, Mapping};
9
10/// Trait implemented by messages that can be unwrapped.
11pub trait Unwrap {
12    /// Unwrapped message type.
13    type Output;
14
15    /// Unwrap message.
16    fn unwrap(self) -> Self::Output;
17}
18
19/// Transforming transport into one unwrapping incoming messages.
20pub trait Unwrapping<Incoming, Outgoing, Error>:
21    dnet_base::Transport<Incoming, Outgoing, Error> + Sized + Unpin
22where
23    Incoming: Unwrap,
24    Error: std::error::Error,
25{
26    /// Convert transport into one unwrapping incoming messages.
27    fn unwrapping(
28        self,
29    ) -> Mapping<
30        Self,
31        Incoming,
32        Outgoing,
33        fn(Incoming) -> <Incoming as Unwrap>::Output,
34        fn(Outgoing) -> Outgoing,
35        Error,
36    > {
37        #[allow(unused_mut)]
38        let mut transport: Mapping<
39            Self,
40            Incoming,
41            Outgoing,
42            fn(Incoming) -> <Incoming as Unwrap>::Output,
43            fn(Outgoing) -> Outgoing,
44            Error,
45        > = self.unmap(Unwrap::unwrap);
46
47        #[cfg(feature = "logging")]
48        transport.with_logger_mut(|logger| logger.override_kind_with_str("Unwrapping"));
49
50        transport
51    }
52}
53
54impl<T, Incoming, Outgoing, Error> Unwrapping<Incoming, Outgoing, Error> for T
55where
56    T: dnet_base::Transport<Incoming, Outgoing, Error> + Unpin,
57    Incoming: Unwrap,
58    Error: std::error::Error,
59{
60}