try_again/
delay_executor.rs

1use crate::StdDuration;
2use std::fmt::Debug;
3
4pub trait DelayExecutor<Delay>: Debug {
5    fn delay_by(&self, by: Delay);
6}
7
8#[cfg(feature = "async")]
9pub trait AsyncDelayExecutor<Delay>: Debug {
10    #[allow(async_fn_in_trait)]
11    async fn delay_by(&self, by: Delay);
12}
13
14#[derive(Debug, Clone, Copy)]
15pub struct ThreadSleep;
16
17impl<Delay: Into<StdDuration>> DelayExecutor<Delay> for ThreadSleep {
18    fn delay_by(&self, delay: Delay) {
19        std::thread::sleep(delay.into())
20    }
21}
22
23#[derive(Debug, Clone, Copy)]
24#[cfg(feature = "async-tokio")]
25pub struct TokioSleep;
26
27#[cfg(feature = "async-tokio")]
28impl<Delay: Into<StdDuration>> AsyncDelayExecutor<Delay> for TokioSleep {
29    async fn delay_by(&self, delay: Delay) {
30        tokio::time::sleep(delay.into()).await
31    }
32}