async_std_resolver/
time.rs

1// Copyright 2015-2020 Benjamin Fry <benjaminfry@me.com>
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// https://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// https://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use std::future::Future;
9use std::time::Duration;
10
11use async_trait::async_trait;
12use hickory_resolver::proto::Time;
13
14/// AsyncStd backed timer implementation
15#[derive(Clone, Copy)]
16pub struct AsyncStdTime;
17
18#[async_trait]
19impl Time for AsyncStdTime {
20    async fn delay_for(duration: Duration) {
21        async_std::task::sleep(duration).await
22    }
23
24    async fn timeout<F: 'static + Future + Send>(
25        duration: Duration,
26        future: F,
27    ) -> Result<F::Output, std::io::Error> {
28        async_std::future::timeout(duration, future)
29            .await
30            .map_err(move |_| std::io::Error::new(std::io::ErrorKind::TimedOut, "future timed out"))
31    }
32}