bitbox_api/
runtime.rs

1use async_trait::async_trait;
2
3#[cfg_attr(feature = "multithreaded", async_trait)]
4#[cfg_attr(not(feature="multithreaded"), async_trait(?Send))]
5pub trait Runtime {
6    async fn sleep(dur: std::time::Duration);
7}
8
9/// Assumes no particular async runtime. Uses std::thread::sleep to sleep.
10/// Useful if using futures::executor::block_on() to run synchronously.
11pub struct DefaultRuntime;
12
13#[cfg_attr(feature = "multithreaded", async_trait)]
14#[cfg_attr(not(feature="multithreaded"), async_trait(?Send))]
15impl Runtime for DefaultRuntime {
16    async fn sleep(dur: std::time::Duration) {
17        std::thread::sleep(dur);
18    }
19}
20
21#[cfg(feature = "tokio")]
22pub struct TokioRuntime;
23
24#[cfg(feature = "tokio")]
25#[cfg_attr(feature = "multithreaded", async_trait)]
26#[cfg_attr(not(feature="multithreaded"), async_trait(?Send))]
27impl Runtime for TokioRuntime {
28    async fn sleep(dur: std::time::Duration) {
29        tokio::time::sleep(dur).await
30    }
31}