Skip to content

Commit

Permalink
Update all dependencies to be Tokio 1.0 compatible
Browse files Browse the repository at this point in the history
Update `reqwest`, `tokio-dns-proto` and `tokio-dns-resolver` to all use
the Tokio 1.0 variants now they have been released.
  • Loading branch information
alexander-jackson committed Jan 7, 2021
1 parent db1139a commit b26f23c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ chrono = "0.4.7"
derivative = "2.1.1"
err-derive = "0.2.3"
futures = "0.3.5"
futures-intrusive = "0.3.0"
hex = "0.4.0"
hmac = "~0.7.1"
lazy_static = "1.4.0"
Expand All @@ -49,8 +50,8 @@ stringprep = "0.1.2"
strsim = "0.10.0"
take_mut = "0.2.2"
time = "0.1.42"
trust-dns-proto = "0.19.4"
trust-dns-resolver = "0.19.5"
trust-dns-proto = "0.20.0"
trust-dns-resolver = "0.20.0"
typed-builder = "0.4.0"
version_check = "0.9.1"
webpki = "0.21.0"
Expand All @@ -65,7 +66,7 @@ version = "0.3.0"
default-features = false

[dependencies.reqwest]
version = "0.10.6"
version = "0.11"
optional = true
default-features = false
features = ["json", "rustls-tls"]
Expand Down
56 changes: 38 additions & 18 deletions src/runtime/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use std::{future::Future, net::SocketAddr, time::Duration};
use std::{
future::Future,
net::SocketAddr,
task::{Context, Poll},
time::Duration,
};

use async_trait::async_trait;
use tokio::io::ReadBuf;
use trust_dns_proto::error::ProtoError;
use trust_dns_resolver::{
config::ResolverConfig,
Expand Down Expand Up @@ -29,10 +35,8 @@ pub(crate) struct AsyncResolver {
impl AsyncResolver {
pub(crate) async fn new(config: Option<ResolverConfig>) -> Result<Self> {
let resolver = match config {
Some(config) => {
TrustDnsResolver::new(config, Default::default(), crate::RUNTIME).await?
}
None => TrustDnsResolver::from_system_conf(crate::RUNTIME).await?,
Some(config) => TrustDnsResolver::new(config, Default::default(), crate::RUNTIME)?,
None => TrustDnsResolver::from_system_conf(crate::RUNTIME)?,
};
Ok(Self { resolver })
}
Expand Down Expand Up @@ -90,12 +94,14 @@ impl trust_dns_resolver::name_server::Spawn for AsyncRuntime {
}
}

impl trust_dns_proto::tcp::DnsTcpStream for AsyncTcpStream {
type Time = AsyncRuntime;
}

#[async_trait]
impl trust_dns_proto::tcp::Connect for AsyncTcpStream {
type Transport = Self;

/// connect to tcp
async fn connect(addr: SocketAddr) -> std::io::Result<Self::Transport> {
async fn connect(addr: SocketAddr) -> std::io::Result<Self> {
AsyncTcpStream::connect_socket_addr(&addr, None)
.await
.map_err(Error::into_io_error)
Expand All @@ -114,7 +120,9 @@ enum AsyncUdpSocket {

#[async_trait]
impl trust_dns_proto::udp::UdpSocket for AsyncUdpSocket {
async fn bind(addr: &SocketAddr) -> std::io::Result<Self> {
type Time = AsyncRuntime;

async fn bind(addr: SocketAddr) -> std::io::Result<Self> {
#[cfg(feature = "tokio-runtime")]
use tokio::net::UdpSocket;

Expand All @@ -125,26 +133,38 @@ impl trust_dns_proto::udp::UdpSocket for AsyncUdpSocket {
Ok(socket.into())
}

/// Receive data from the socket and returns the number of bytes read and the address from
/// where the data came on success.
async fn recv_from(&mut self, buf: &mut [u8]) -> std::io::Result<(usize, SocketAddr)> {
fn poll_recv_from(
&self,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<std::io::Result<(usize, SocketAddr)>> {
match self {
#[cfg(feature = "tokio-runtime")]
AsyncUdpSocket::Tokio(ref mut socket) => socket.recv_from(buf).await,
AsyncUdpSocket::Tokio(ref socket) => {
let mut readbuf = ReadBuf::new(buf);

socket
.poll_recv_from(cx, &mut readbuf)
.map(|x| x.map(|y| (readbuf.filled().len(), y)))
}

#[cfg(feature = "async-std-runtime")]
AsyncUdpSocket::AsyncStd(ref mut socket) => socket.recv_from(buf).await,
AsyncUdpSocket::AsyncStd(ref socket) => socket.recv_from(buf).await,
}
}

/// Send data to the given address.
async fn send_to(&mut self, buf: &[u8], target: &SocketAddr) -> std::io::Result<usize> {
fn poll_send_to(
&self,
cx: &mut Context<'_>,
buf: &[u8],
target: SocketAddr,
) -> Poll<std::io::Result<usize>> {
match self {
#[cfg(feature = "tokio-runtime")]
AsyncUdpSocket::Tokio(ref mut socket) => socket.send_to(buf, target).await,
AsyncUdpSocket::Tokio(ref socket) => socket.poll_send_to(cx, buf, target),

#[cfg(feature = "async-std-runtime")]
AsyncUdpSocket::AsyncStd(ref mut socket) => socket.send_to(buf, target).await,
AsyncUdpSocket::AsyncStd(ref socket) => socket.send_to(buf, target).await,
}
}
}
Expand Down

0 comments on commit b26f23c

Please sign in to comment.