Skip to main content

rs_matter_stack/
nal.rs

1/// Re-export the `edge-nal` crate
2pub use edge_nal::*;
3
4/// The network stack used by `rs-matter-stack`
5///
6/// Depending on the platform and features enabled, the stack
7/// might or might not support certain protocols.
8pub trait NetStack {
9    type UdpBind<'a>: UdpBind
10    where
11        Self: 'a;
12    type UdpConnect<'a>: UdpConnect
13    where
14        Self: 'a;
15
16    type TcpBind<'a>: TcpBind
17    where
18        Self: 'a;
19    type TcpConnect<'a>: TcpConnect
20    where
21        Self: 'a;
22
23    type Dns<'a>: Dns
24    where
25        Self: 'a;
26
27    /// Return a UDP Bind implementation, if supported by the network stack
28    fn udp_bind(&self) -> Option<Self::UdpBind<'_>>;
29
30    /// Return a UDP Connect implementation, if supported by the network stack
31    fn udp_connect(&self) -> Option<Self::UdpConnect<'_>>;
32
33    /// Return a TCP Bind implementation, if supported by the network stack
34    fn tcp_bind(&self) -> Option<Self::TcpBind<'_>>;
35
36    /// Return a TCP Connect implementation, if supported by the network stack
37    fn tcp_connect(&self) -> Option<Self::TcpConnect<'_>>;
38
39    /// Return a DNS implementation, if supported by the network stack
40    fn dns(&self) -> Option<Self::Dns<'_>>;
41}
42
43impl<T> NetStack for &T
44where
45    T: NetStack,
46{
47    type UdpBind<'a>
48        = T::UdpBind<'a>
49    where
50        Self: 'a;
51    type UdpConnect<'a>
52        = T::UdpConnect<'a>
53    where
54        Self: 'a;
55
56    type TcpBind<'a>
57        = T::TcpBind<'a>
58    where
59        Self: 'a;
60    type TcpConnect<'a>
61        = T::TcpConnect<'a>
62    where
63        Self: 'a;
64
65    type Dns<'a>
66        = T::Dns<'a>
67    where
68        Self: 'a;
69
70    fn udp_bind(&self) -> Option<Self::UdpBind<'_>> {
71        (*self).udp_bind()
72    }
73
74    fn udp_connect(&self) -> Option<Self::UdpConnect<'_>> {
75        (*self).udp_connect()
76    }
77
78    fn tcp_bind(&self) -> Option<Self::TcpBind<'_>> {
79        (*self).tcp_bind()
80    }
81
82    fn tcp_connect(&self) -> Option<Self::TcpConnect<'_>> {
83        (*self).tcp_connect()
84    }
85
86    fn dns(&self) -> Option<Self::Dns<'_>> {
87        (*self).dns()
88    }
89}
90
91/// Re-export the `edge-nal-std` crate
92#[cfg(feature = "std")]
93pub mod std {
94    pub use edge_nal_std::*;
95
96    impl super::NetStack for Stack {
97        type UdpBind<'a>
98            = &'a Stack
99        where
100            Self: 'a;
101        type UdpConnect<'a>
102            = &'a Stack
103        where
104            Self: 'a;
105
106        type TcpBind<'a>
107            = &'a Stack
108        where
109            Self: 'a;
110        type TcpConnect<'a>
111            = &'a Stack
112        where
113            Self: 'a;
114
115        type Dns<'a>
116            = &'a Stack
117        where
118            Self: 'a;
119
120        fn udp_bind(&self) -> Option<Self::UdpBind<'_>> {
121            Some(self)
122        }
123
124        fn udp_connect(&self) -> Option<Self::UdpConnect<'_>> {
125            Some(self)
126        }
127
128        fn tcp_bind(&self) -> Option<Self::TcpBind<'_>> {
129            Some(self)
130        }
131
132        fn tcp_connect(&self) -> Option<Self::TcpConnect<'_>> {
133            Some(self)
134        }
135
136        fn dns(&self) -> Option<Self::Dns<'_>> {
137            Some(self)
138        }
139    }
140}