Skip to main content

edge_nal/
noop.rs

1//! A no-op implementation of all `edge-nal` traits that panics when used.
2
3use core::convert::Infallible;
4use core::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
5
6use embedded_io_async::{ErrorType, Read, Write};
7
8use crate::{
9    AddrType, Close, Dns, MulticastV4, MulticastV6, Readable, TcpAccept, TcpBind, TcpConnect,
10    TcpShutdown, TcpSplit, UdpBind, UdpConnect, UdpReceive, UdpSend, UdpSplit, UdpSplitMulticast,
11};
12
13/// A type that implements all `edge-nal` traits but does not support any operation
14/// and panics when any method is called.
15pub struct NoopNet;
16
17impl UdpBind for NoopNet {
18    type Error = Infallible;
19
20    type Socket<'a>
21        = NoopNet
22    where
23        Self: 'a;
24
25    async fn bind(&self, _local: SocketAddr) -> Result<Self::Socket<'_>, Self::Error> {
26        panic!("UDP bind not supported")
27    }
28}
29
30impl UdpConnect for NoopNet {
31    type Error = Infallible;
32
33    type Socket<'a>
34        = NoopNet
35    where
36        Self: 'a;
37
38    async fn connect(
39        &self,
40        _local: SocketAddr,
41        _remote: SocketAddr,
42    ) -> Result<Self::Socket<'_>, Self::Error> {
43        panic!("UDP connect not supported")
44    }
45}
46
47impl TcpBind for NoopNet {
48    type Error = Infallible;
49
50    type Accept<'a>
51        = NoopNet
52    where
53        Self: 'a;
54
55    async fn bind(&self, _local: SocketAddr) -> Result<Self::Accept<'_>, Self::Error> {
56        panic!("TCP bind not supported")
57    }
58}
59
60impl TcpAccept for NoopNet {
61    type Error = Infallible;
62
63    type Socket<'a>
64        = NoopNet
65    where
66        Self: 'a;
67
68    async fn accept(&self) -> Result<(SocketAddr, Self::Socket<'_>), Self::Error> {
69        panic!("TCP accept not supported")
70    }
71}
72
73impl TcpConnect for NoopNet {
74    type Error = Infallible;
75
76    type Socket<'a>
77        = NoopNet
78    where
79        Self: 'a;
80
81    async fn connect(&self, _remote: SocketAddr) -> Result<Self::Socket<'_>, Self::Error> {
82        panic!("TCP connect not supported")
83    }
84}
85
86impl Dns for NoopNet {
87    type Error = Infallible;
88
89    async fn get_host_by_name(
90        &self,
91        _host: &str,
92        _addr_type: AddrType,
93    ) -> Result<IpAddr, Self::Error> {
94        panic!("DNS get_host_by_name not supported")
95    }
96
97    async fn get_host_by_address(
98        &self,
99        _addr: IpAddr,
100        _result: &mut [u8],
101    ) -> Result<usize, Self::Error> {
102        panic!("DNS get_host_by_address not supported")
103    }
104}
105
106impl ErrorType for NoopNet {
107    type Error = Infallible;
108}
109
110impl Readable for NoopNet {
111    async fn readable(&mut self) -> Result<(), Self::Error> {
112        panic!("Readable not supported")
113    }
114}
115
116impl UdpSend for NoopNet {
117    async fn send(&mut self, _remote: SocketAddr, _data: &[u8]) -> Result<(), Self::Error> {
118        panic!("UDP send not supported")
119    }
120}
121
122impl UdpReceive for NoopNet {
123    async fn receive(&mut self, _buffer: &mut [u8]) -> Result<(usize, SocketAddr), Self::Error> {
124        panic!("UDP receive not supported")
125    }
126}
127
128impl UdpSplit for NoopNet {
129    type Receive<'a> = Self;
130    type Send<'a> = Self;
131
132    fn split(&mut self) -> (Self::Receive<'_>, Self::Send<'_>) {
133        panic!("UDP split not supported")
134    }
135}
136
137impl UdpSplitMulticast for NoopNet {
138    type MulticastV4<'a> = Self;
139    type MulticastV6<'a> = Self;
140
141    fn split_multicast(
142        &mut self,
143    ) -> (
144        Self::Receive<'_>,
145        Self::Send<'_>,
146        Self::MulticastV4<'_>,
147        Self::MulticastV6<'_>,
148    ) {
149        panic!("UDP split multicast not supported")
150    }
151}
152
153impl MulticastV4 for NoopNet {
154    async fn join_v4(
155        &mut self,
156        _multicast_addr: Ipv4Addr,
157        _interface: Ipv4Addr,
158    ) -> Result<(), Self::Error> {
159        panic!("Multicast join not supported")
160    }
161
162    async fn leave_v4(
163        &mut self,
164        _multicast_addr: Ipv4Addr,
165        _interface: Ipv4Addr,
166    ) -> Result<(), Self::Error> {
167        panic!("Multicast leave not supported")
168    }
169}
170
171impl MulticastV6 for NoopNet {
172    async fn join_v6(
173        &mut self,
174        _multicast_addr: Ipv6Addr,
175        _interface: u32,
176    ) -> Result<(), Self::Error> {
177        panic!("Multicast join not supported")
178    }
179
180    async fn leave_v6(
181        &mut self,
182        _multicast_addr: Ipv6Addr,
183        _interface: u32,
184    ) -> Result<(), Self::Error> {
185        panic!("Multicast leave not supported")
186    }
187}
188
189impl Read for NoopNet {
190    async fn read(&mut self, _buf: &mut [u8]) -> Result<usize, Self::Error> {
191        panic!("Read not supported")
192    }
193}
194
195impl Write for NoopNet {
196    async fn write(&mut self, _buf: &[u8]) -> Result<usize, Self::Error> {
197        panic!("Write not supported")
198    }
199
200    async fn flush(&mut self) -> Result<(), Self::Error> {
201        panic!("Flush not supported")
202    }
203}
204
205impl TcpSplit for NoopNet {
206    type Read<'a> = Self;
207    type Write<'a> = Self;
208
209    fn split(&mut self) -> (Self::Read<'_>, Self::Write<'_>) {
210        panic!("TCP split not supported")
211    }
212}
213
214impl TcpShutdown for NoopNet {
215    async fn close(&mut self, _what: Close) -> Result<(), Self::Error> {
216        panic!("TCP shutdown not supported")
217    }
218
219    async fn abort(&mut self) -> Result<(), Self::Error> {
220        panic!("TCP abort not supported")
221    }
222}