use async_trait::async_trait;
use fuchsia_async::{self as fasync, TimeoutExt};
use futures::future::FutureExt;
use futures::Future;
use std::io;
use std::time::Duration;
use trust_dns_proto::{Executor, Time};
pub mod async_resolver;
pub mod config;
pub mod tcp;
pub mod test_util;
pub mod udp;
pub const DEFAULT_PORT: u16 = 53;
pub struct FuchsiaExec(fasync::LocalExecutor);
impl FuchsiaExec {
pub fn new() -> Result<Self, zx::Status> {
Ok(Self(fasync::LocalExecutor::new()))
}
pub fn get(&mut self) -> &mut fasync::LocalExecutor {
&mut self.0
}
}
impl Executor for FuchsiaExec {
fn new() -> Self {
Self::new().expect("failed to create fuchsia executor")
}
fn block_on<F: Future>(&mut self, future: F) -> F::Output {
self.0.run_singlethreaded(future)
}
}
pub struct FuchsiaTime;
#[async_trait]
impl Time for FuchsiaTime {
async fn timeout<F: 'static + Future + Send>(
duration: Duration,
future: F,
) -> Result<F::Output, io::Error> {
let nanos = i64::try_from(duration.as_nanos()).expect("failed to cast the input into i64 ");
let zx_duration = zx::MonotonicDuration::from_nanos(nanos);
future
.map(|output| Ok(output))
.on_timeout(fasync::MonotonicInstant::after(zx_duration), || {
Err(io::Error::new(io::ErrorKind::TimedOut, "future timed out"))
})
.await
}
async fn delay_for(duration: Duration) -> () {
let nanos = i64::try_from(duration.as_nanos()).expect("failed to cast the input into i64");
let zx_duration = zx::MonotonicDuration::from_nanos(nanos);
fasync::Timer::new(fasync::MonotonicInstant::after(zx_duration)).await
}
}