use std::future::Future;
use std::io;
use std::net::SocketAddr;
#[doc(hidden)]
pub trait Transport: Send + Sync + 'static {
fn send_to(
&self,
bytes: &[u8],
to: SocketAddr,
) -> impl Future<Output = io::Result<usize>> + Send;
fn recv_from(
&self,
buf: &mut [u8],
) -> impl Future<Output = io::Result<(usize, SocketAddr)>> + Send;
fn resolve(
&self,
host: &str,
port: u16,
) -> impl Future<Output = io::Result<Vec<SocketAddr>>> + Send {
let target = (host.to_owned(), port);
async move { Ok(tokio::net::lookup_host(target).await?.collect()) }
}
}
impl Transport for tokio::net::UdpSocket {
fn send_to(
&self,
bytes: &[u8],
to: SocketAddr,
) -> impl Future<Output = io::Result<usize>> + Send {
tokio::net::UdpSocket::send_to(self, bytes, to)
}
fn recv_from(
&self,
buf: &mut [u8],
) -> impl Future<Output = io::Result<(usize, SocketAddr)>> + Send {
tokio::net::UdpSocket::recv_from(self, buf)
}
}
#[cfg(feature = "turmoil")]
impl Transport for turmoil::net::UdpSocket {
fn send_to(
&self,
bytes: &[u8],
to: SocketAddr,
) -> impl Future<Output = io::Result<usize>> + Send {
turmoil::net::UdpSocket::send_to(self, bytes, to)
}
fn recv_from(
&self,
buf: &mut [u8],
) -> impl Future<Output = io::Result<(usize, SocketAddr)>> + Send {
turmoil::net::UdpSocket::recv_from(self, buf)
}
fn resolve(
&self,
host: &str,
port: u16,
) -> impl Future<Output = io::Result<Vec<SocketAddr>>> + Send {
let found = SocketAddr::new(turmoil::lookup(host), port);
async move { Ok(vec![found]) }
}
}