Skip to content

Commit 6c5bf68

Browse files
committed
Runtime: add to_socket_addrs
1 parent 4a3b48f commit 6c5bf68

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/runtime.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
use crate::{AsyncIOHandle, Executor, IOHandle, Reactor, RuntimeKit, Task, sys::IO};
1+
use crate::{
2+
AsyncIOHandle, AsyncToSocketAddrs, Executor, IOHandle, Reactor, RuntimeKit, Task, sys::IO,
3+
};
24
use async_trait::async_trait;
35
use futures_core::Stream;
46
use std::{
57
fmt,
68
future::Future,
79
io,
8-
net::SocketAddr,
10+
net::{SocketAddr, ToSocketAddrs},
911
pin::Pin,
1012
time::{Duration, Instant},
1113
};
@@ -21,6 +23,20 @@ impl<RK: RuntimeKit + 'static> Runtime<RK> {
2123
pub fn new(kit: RK) -> Self {
2224
Self { kit }
2325
}
26+
27+
/// Asynchronously resolve the given domain name
28+
pub fn to_socket_addrs<A: ToSocketAddrs + Send + 'static>(
29+
&self,
30+
addrs: A,
31+
) -> impl AsyncToSocketAddrs
32+
where
33+
<A as std::net::ToSocketAddrs>::Iter: Iterator<Item = SocketAddr> + Send + 'static,
34+
{
35+
SocketAddrsResolver {
36+
runtime: self,
37+
addrs,
38+
}
39+
}
2440
}
2541

2642
impl<RK: RuntimeKit + 'static> From<RK> for Runtime<RK> {
@@ -131,3 +147,21 @@ impl<E: Executor + Sync, R: Reactor + Sync> Reactor for RuntimeParts<E, R> {
131147
self.reactor.tcp_connect(addr).await
132148
}
133149
}
150+
151+
struct SocketAddrsResolver<'a, RK: RuntimeKit + 'static, A: ToSocketAddrs + Send + 'static> {
152+
runtime: &'a Runtime<RK>,
153+
addrs: A,
154+
}
155+
156+
impl<'a, RK: RuntimeKit + 'static, A: ToSocketAddrs + Send + 'static> AsyncToSocketAddrs
157+
for SocketAddrsResolver<'a, RK, A>
158+
where
159+
<A as ToSocketAddrs>::Iter: Iterator<Item = SocketAddr> + Send + 'static,
160+
{
161+
fn to_socket_addrs(
162+
self,
163+
) -> impl Future<Output = io::Result<impl Iterator<Item = SocketAddr> + Send>> + Send {
164+
let SocketAddrsResolver { runtime, addrs } = self;
165+
runtime.spawn_blocking(move || addrs.to_socket_addrs())
166+
}
167+
}

0 commit comments

Comments
 (0)