-
Notifications
You must be signed in to change notification settings - Fork 341
/
Copy pathaddr.rs
188 lines (157 loc) · 5.78 KB
/
addr.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std::future;
use std::future::Future;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::net::{SocketAddr, SocketAddrV4, SocketAddrV6};
use std::pin::Pin;
use crate::io;
use crate::task::{spawn_blocking};
use crate::utils::Context as ErrorContext;
/// Converts or resolves addresses to [`SocketAddr`] values.
///
/// This trait is an async version of [`std::net::ToSocketAddrs`].
///
/// [`std::net::ToSocketAddrs`]: https://doc.rust-lang.org/std/net/trait.ToSocketAddrs.html
/// [`SocketAddr`]: enum.SocketAddr.html
///
/// # Examples
///
/// ```
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::net::ToSocketAddrs;
///
/// let addr = "localhost:8080".to_socket_addrs().await?.next().unwrap();
/// println!("resolved: {:?}", addr);
/// #
/// # Ok(()) }) }
/// ```
pub trait ToSocketAddrs {
/// Returned iterator over socket addresses which this type may correspond to.
type Iter: Iterator<Item = SocketAddr>;
/// Converts this object to an iterator of resolved `SocketAddr`s.
///
/// The returned iterator may not actually yield any values depending on the outcome of any
/// resolution performed.
///
/// Note that this function may block a backend thread while resolution is performed.
fn to_socket_addrs<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = io::Result<Self::Iter>> + 'a + Send>>;
}
impl ToSocketAddrs for SocketAddr {
type Iter = std::option::IntoIter<SocketAddr>;
fn to_socket_addrs<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = io::Result<Self::Iter>> + 'a + Send>> {
Box::pin(future::ready(Ok(Some(*self).into_iter())))
}
}
impl ToSocketAddrs for SocketAddrV4 {
type Iter = std::option::IntoIter<SocketAddr>;
fn to_socket_addrs<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = io::Result<Self::Iter>> + 'a + Send>> {
Box::pin(future::ready(Ok(Some(SocketAddr::V4(*self)).into_iter())))
}
}
impl ToSocketAddrs for SocketAddrV6 {
type Iter = std::option::IntoIter<SocketAddr>;
fn to_socket_addrs<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = io::Result<Self::Iter>> + 'a + Send>> {
Box::pin(future::ready(Ok(Some(SocketAddr::V6(*self)).into_iter())))
}
}
impl ToSocketAddrs for (IpAddr, u16) {
type Iter = std::option::IntoIter<SocketAddr>;
fn to_socket_addrs<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = io::Result<Self::Iter>> + 'a + Send>> {
let (ip, port) = *self;
match ip {
IpAddr::V4(a) => Box::pin(future::ready(Ok(Some(SocketAddr::V4(SocketAddrV4::new(a, port))).into_iter()))),
IpAddr::V6(a) => Box::pin(future::ready(Ok(Some(SocketAddr::V6(SocketAddrV6::new(a, port, 0, 0))).into_iter()))),
}
}
}
impl ToSocketAddrs for (Ipv4Addr, u16) {
type Iter = std::option::IntoIter<SocketAddr>;
fn to_socket_addrs<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = io::Result<Self::Iter>> + 'a + Send>> {
let (ip, port) = *self;
Box::pin(future::ready(Ok(Some(SocketAddr::V4(SocketAddrV4::new(ip, port))).into_iter())))
}
}
impl ToSocketAddrs for (Ipv6Addr, u16) {
type Iter = std::option::IntoIter<SocketAddr>;
fn to_socket_addrs<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = io::Result<Self::Iter>> + 'a + Send>> {
let (ip, port) = *self;
Box::pin(future::ready(Ok(Some(SocketAddr::V6(SocketAddrV6::new(ip, port, 0, 0))).into_iter())))
}
}
impl ToSocketAddrs for (&str, u16) {
type Iter = std::vec::IntoIter<SocketAddr>;
fn to_socket_addrs<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = io::Result<Self::Iter>> + 'a + Send>> {
let (host, port) = *self;
if let Ok(addr) = host.parse::<Ipv4Addr>() {
let addr = SocketAddrV4::new(addr, port);
return Box::pin(future::ready(Ok(vec![SocketAddr::V4(addr)].into_iter())));
}
if let Ok(addr) = host.parse::<Ipv6Addr>() {
let addr = SocketAddrV6::new(addr, port, 0, 0);
return Box::pin(future::ready(Ok(vec![SocketAddr::V6(addr)].into_iter())));
}
let host = host.to_string();
let task = spawn_blocking(move || {
let addr = (host.as_str(), port);
std::net::ToSocketAddrs::to_socket_addrs(&addr)
.context(|| format!("could not resolve address `{:?}`", addr))
});
Box::pin(task)
}
}
impl ToSocketAddrs for str {
type Iter = std::vec::IntoIter<SocketAddr>;
fn to_socket_addrs<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = io::Result<Self::Iter>> + 'a + Send>> {
if let Ok(addr) = self.parse() {
return Box::pin(future::ready(Ok(vec![addr].into_iter())));
}
let addr = self.to_string();
let task = spawn_blocking(move || {
std::net::ToSocketAddrs::to_socket_addrs(addr.as_str())
.context(|| format!("could not resolve address `{:?}`", addr))
});
Box::pin(task)
}
}
impl<'b> ToSocketAddrs for &'b [SocketAddr] {
type Iter = std::iter::Cloned<std::slice::Iter<'b, SocketAddr>>;
fn to_socket_addrs<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = io::Result<Self::Iter>> + 'a + Send>> {
Box::pin(future::ready(Ok(self.iter().cloned())))
}
}
impl<T: ToSocketAddrs + ?Sized> ToSocketAddrs for &T {
type Iter = T::Iter;
fn to_socket_addrs<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = io::Result<Self::Iter>> + 'a + Send>> {
(**self).to_socket_addrs()
}
}
impl ToSocketAddrs for String {
type Iter = std::vec::IntoIter<SocketAddr>;
fn to_socket_addrs<'a>(
&'a self,
) -> Pin<Box<dyn Future<Output = io::Result<Self::Iter>> + 'a + Send>> {
(&**self).to_socket_addrs()
}
}