bitbox_api/
runtime.rs

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