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