1use std::net::{
2 SocketAddr,
3 SocketAddrV4,
4 SocketAddrV6,
5 IpAddr,
6};
7use std::str::FromStr;
8use std::path::Path;
9
10use async_trait::async_trait;
11
12
13
14#[cfg(feature = "tokio-rt")]
16#[cfg_attr(docsrs, doc(cfg(feature = "tokio-rt")))]
17mod tokio;
18#[cfg(feature = "tokio-rt")]
19#[cfg_attr(docsrs, doc(cfg(feature = "tokio-rt")))]
20pub use self::tokio::*;
21
22#[cfg(feature = "async-std-rt")]
24#[cfg_attr(docsrs, doc(cfg(feature = "async-std-rt")))]
25mod async_std;
26#[cfg(feature = "async-std-rt")]
27#[cfg_attr(docsrs, doc(cfg(feature = "async-std-rt")))]
28pub use self::async_std::*;
29
30
31
32#[cfg(unix)]
34#[cfg_attr(docsrs, doc(cfg(unix)))]
35pub trait UnixSocketAddr {
36 fn is_unnamed(&self) -> bool;
38
39 fn as_pathname(&self) -> Option<&Path>;
41}
42
43#[cfg(unix)]
44#[cfg_attr(docsrs, doc(cfg(unix)))]
45impl UnixSocketAddr for std::os::unix::net::SocketAddr {
46 fn is_unnamed(&self) -> bool {
47 self.is_unnamed()
48 }
49
50 fn as_pathname(&self) -> Option<&Path> {
51 self.as_pathname()
52 }
53}
54
55
56
57#[async_trait]
61pub trait ToSocketAddrs {
62 type Iter: Iterator<Item = SocketAddr>;
63
64 async fn to_socket_addrs(self) -> Self::Iter;
65}
66
67#[async_trait]
68impl<I> ToSocketAddrs for (I, u16)
69where
70 I: Into<IpAddr> + Send,
71{
72 type Iter = std::array::IntoIter<SocketAddr, 1>;
73
74 async fn to_socket_addrs(self) -> Self::Iter {
75 let addr: SocketAddr = self.into();
76
77 IntoIterator::into_iter([addr])
78 }
79}
80
81#[async_trait]
82impl ToSocketAddrs for SocketAddrV4 {
83 type Iter = std::array::IntoIter<SocketAddr, 1>;
84
85 async fn to_socket_addrs(self) -> Self::Iter {
86 let addr: SocketAddr = self.into();
87
88 IntoIterator::into_iter([addr])
89 }
90}
91
92#[async_trait]
93impl ToSocketAddrs for SocketAddrV6 {
94 type Iter = std::array::IntoIter<SocketAddr, 1>;
95
96 async fn to_socket_addrs(self) -> Self::Iter {
97 let addr: SocketAddr = self.into();
98
99 IntoIterator::into_iter([addr])
100 }
101}
102
103#[async_trait]
104impl ToSocketAddrs for String {
105 type Iter = std::vec::IntoIter<SocketAddr>;
106
107 async fn to_socket_addrs(self) -> Self::Iter {
108 let addr: Vec<SocketAddr> = match SocketAddr::from_str(&self) {
109 Ok(addr) => vec![addr],
110 Err(_) => Vec::new(),
111 };
112
113 IntoIterator::into_iter(addr)
114 }
115}
116
117#[async_trait]
118impl ToSocketAddrs for &str {
119 type Iter = std::vec::IntoIter<SocketAddr>;
120
121 async fn to_socket_addrs(self) -> Self::Iter {
122 let addr: Vec<SocketAddr> = match SocketAddr::from_str(self) {
123 Ok(addr) => vec![addr],
124 Err(_) => Vec::new(),
125 };
126
127 IntoIterator::into_iter(addr)
128 }
129}
130
131#[async_trait]
132impl ToSocketAddrs for &[SocketAddr] {
133 type Iter = std::vec::IntoIter<SocketAddr>;
134
135 async fn to_socket_addrs(self) -> Self::Iter {
136 let addr: Vec<SocketAddr> = self.iter().map(|&addr| addr).collect();
137
138 IntoIterator::into_iter(addr)
139 }
140}
141
142
143
144#[async_trait]
146pub trait TcpStream: Sized {
147 async fn connect<A: ToSocketAddrs + Send>(addrs: A) -> std::io::Result<Self>;
156
157 async fn peek(&self, buf: &mut [u8]) -> std::io::Result<usize>;
165
166 fn peer_addr(&self) -> std::io::Result<SocketAddr>;
168
169 fn local_addr(&self) -> std::io::Result<SocketAddr>;
171
172 fn nodelay(&self) -> std::io::Result<bool>;
178
179 fn set_nodelay(&self, nodelay: bool) -> std::io::Result<()>;
187
188 fn ttl(&self) -> std::io::Result<u32>;
194
195 fn set_ttl(&self, ttl: u32) -> std::io::Result<()>;
200}
201
202
203
204#[async_trait]
206pub trait TcpListener: Sized {
207 type TcpStream: TcpStream;
208
209 async fn bind<A: ToSocketAddrs + Send>(addrs: A) -> std::io::Result<Self>;
218
219 async fn accept(&self) -> std::io::Result<(Self::TcpStream, SocketAddr)>;
223
224 fn local_addr(&self) -> std::io::Result<SocketAddr>;
229}
230
231
232
233
234#[cfg(unix)]
236#[cfg_attr(docsrs, doc(cfg(unix)))]
237#[async_trait]
238pub trait UnixStream: Sized {
239 type SocketAddr: UnixSocketAddr;
240
241 async fn connect<P: AsRef<Path> + Send>(path: P) -> std::io::Result<Self>;
243
244 fn pair() -> std::io::Result<(Self, Self)>;
248
249 fn peer_addr(&self) -> std::io::Result<Self::SocketAddr>;
251
252 fn local_addr(&self) -> std::io::Result<Self::SocketAddr>;
254}
255
256
257
258#[cfg(unix)]
260#[cfg_attr(docsrs, doc(cfg(unix)))]
261#[async_trait]
262pub trait UnixListener: Sized {
263 type UnixStream: UnixStream;
264 type SocketAddr: UnixSocketAddr;
265
266 async fn bind<P: AsRef<Path> + Send>(path: P) -> std::io::Result<Self>;
268
269 async fn accept(&self) -> std::io::Result<(Self::UnixStream, Self::SocketAddr)>;
273
274 fn local_addr(&self) -> std::io::Result<Self::SocketAddr>;
276}