port over all tests

This commit is contained in:
Benjamin Fry
2020-04-10 14:57:29 -07:00
parent 87efb45bf1
commit fd356c258b
11 changed files with 457 additions and 129 deletions

View File

@@ -0,0 +1,81 @@
// Copyright 2015-2020 Benjamin Fry <benjaminfry@me.com>
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use std::io;
use std::net::SocketAddr;
use std::pin::Pin;
use async_trait::async_trait;
use futures::io::{AsyncRead, AsyncWrite};
use trust_dns_resolver::proto::tcp::Connect;
use trust_dns_resolver::proto::udp::UdpSocket;
pub struct AsyncStdUdpSocket(async_std::net::UdpSocket);
#[async_trait]
impl UdpSocket for AsyncStdUdpSocket {
async fn bind(addr: &SocketAddr) -> io::Result<Self> {
async_std::net::UdpSocket::bind(addr)
.await
.map(AsyncStdUdpSocket)
}
async fn recv_from(&mut self, buf: &mut [u8]) -> io::Result<(usize, SocketAddr)> {
self.0.recv_from(buf).await
}
async fn send_to(&mut self, buf: &[u8], target: &SocketAddr) -> io::Result<usize> {
self.0.send_to(buf, target).await
}
}
pub struct AsyncStdTcpStream(async_std::net::TcpStream);
#[async_trait]
impl Connect for AsyncStdTcpStream {
type Transport = AsyncStdTcpStream;
async fn connect(addr: SocketAddr) -> io::Result<Self::Transport> {
async_std::net::TcpStream::connect(addr)
.await
.map(AsyncStdTcpStream)
}
}
impl AsyncWrite for AsyncStdTcpStream {
fn poll_write(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
bytes: &[u8],
) -> std::task::Poll<std::result::Result<usize, std::io::Error>> {
Pin::new(&mut self.0).poll_write(cx, bytes)
}
fn poll_flush(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<std::result::Result<(), std::io::Error>> {
Pin::new(&mut self.0).poll_flush(cx)
}
fn poll_close(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<std::result::Result<(), std::io::Error>> {
Pin::new(&mut self.0).poll_close(cx)
}
}
impl AsyncRead for AsyncStdTcpStream {
fn poll_read(
mut self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
bytes: &mut [u8],
) -> std::task::Poll<std::result::Result<usize, std::io::Error>> {
Pin::new(&mut self.0).poll_read(cx, bytes)
}
}