bitbox_api/
runtime.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use async_trait::async_trait;

#[cfg_attr(feature = "multithreaded", async_trait)]
#[cfg_attr(not(feature="multithreaded"), async_trait(?Send))]
pub trait Runtime {
    async fn sleep(dur: std::time::Duration);
}

/// Assumes no particular async runtime. Uses std::thread::sleep to sleep.
/// Useful if using futures::executor::block_on() to run synchronously.
pub struct DefaultRuntime;

#[cfg_attr(feature = "multithreaded", async_trait)]
#[cfg_attr(not(feature="multithreaded"), async_trait(?Send))]
impl Runtime for DefaultRuntime {
    async fn sleep(dur: std::time::Duration) {
        std::thread::sleep(dur);
    }
}

#[cfg(feature = "tokio")]
pub struct TokioRuntime;

#[cfg(feature = "tokio")]
#[cfg_attr(feature = "multithreaded", async_trait)]
#[cfg_attr(not(feature="multithreaded"), async_trait(?Send))]
impl Runtime for TokioRuntime {
    async fn sleep(dur: std::time::Duration) {
        tokio::time::sleep(dur).await
    }
}