embedded_nal_async_xtra/stack/
tcp.rs

1use embedded_io_async::ErrorType;
2use embedded_nal_async::SocketAddr;
3
4pub trait TcpSplittableConnection: ErrorType {
5    type Read<'a>: embedded_io_async::Read<Error = Self::Error>
6    where
7        Self: 'a;
8    type Write<'a>: embedded_io_async::Write<Error = Self::Error>
9    where
10        Self: 'a;
11
12    fn split(&mut self) -> Result<(Self::Read<'_>, Self::Write<'_>), Self::Error>;
13}
14
15impl<'t, T> TcpSplittableConnection for &'t mut T
16where
17    T: TcpSplittableConnection + 't,
18{
19    type Read<'a> = T::Read<'a> where Self: 'a;
20
21    type Write<'a> = T::Write<'a> where Self: 'a;
22
23    fn split(&mut self) -> Result<(Self::Read<'_>, Self::Write<'_>), Self::Error> {
24        (**self).split()
25    }
26}
27
28pub trait TcpListen {
29    type Error: embedded_io_async::Error;
30
31    type Acceptor<'m>: TcpAccept<Error = Self::Error>
32    where
33        Self: 'm;
34
35    async fn listen(&self, remote: SocketAddr) -> Result<Self::Acceptor<'_>, Self::Error>;
36}
37
38impl<T> TcpListen for &T
39where
40    T: TcpListen,
41{
42    type Error = T::Error;
43
44    type Acceptor<'m> = T::Acceptor<'m>
45    where Self: 'm;
46
47    async fn listen(&self, remote: SocketAddr) -> Result<Self::Acceptor<'_>, Self::Error> {
48        (*self).listen(remote).await
49    }
50}
51
52impl<T> TcpListen for &mut T
53where
54    T: TcpListen,
55{
56    type Error = T::Error;
57
58    type Acceptor<'m> = T::Acceptor<'m>
59    where Self: 'm;
60
61    async fn listen(&self, remote: SocketAddr) -> Result<Self::Acceptor<'_>, Self::Error> {
62        (**self).listen(remote).await
63    }
64}
65
66pub trait TcpAccept {
67    type Error: embedded_io_async::Error;
68
69    type Connection<'m>: embedded_io_async::Read<Error = Self::Error>
70        + embedded_io_async::Write<Error = Self::Error>
71    where
72        Self: 'm;
73
74    async fn accept(&self) -> Result<Self::Connection<'_>, Self::Error>;
75}
76
77impl<T> TcpAccept for &T
78where
79    T: TcpAccept,
80{
81    type Error = T::Error;
82
83    type Connection<'m> = T::Connection<'m>
84    where Self: 'm;
85
86    async fn accept(&self) -> Result<Self::Connection<'_>, Self::Error> {
87        (**self).accept().await
88    }
89}
90
91impl<T> TcpAccept for &mut T
92where
93    T: TcpAccept,
94{
95    type Error = T::Error;
96
97    type Connection<'m> = T::Connection<'m>
98    where Self: 'm;
99
100    async fn accept(&self) -> Result<Self::Connection<'_>, Self::Error> {
101        (**self).accept().await
102    }
103}