Skip to main content

veilid_tools/
sleep.rs

1use super::*;
2use std::time::Duration;
3
4cfg_if! {
5    if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] {
6        use async_executors::{Bindgen, Timer, YieldNow};
7
8        pub async fn sleep(millis: u32) {
9            if millis == 0 {
10                Bindgen.yield_now().await;
11            } else {
12                Bindgen.sleep(Duration::from_millis(millis.into())).await
13            }
14        }
15
16    } else {
17
18        pub async fn sleep(millis: u32) {
19            if millis == 0 {
20                cfg_if! {
21                    if #[cfg(feature="rt-async-std")] {
22                        async_std::task::yield_now().await;
23                    } else if #[cfg(feature="rt-tokio")] {
24                        tokio::task::yield_now().await;
25                    }
26                }
27            } else {
28                cfg_if! {
29                    if #[cfg(feature="rt-async-std")] {
30                        async_std::task::sleep(Duration::from_millis(u64::from(millis))).await;
31                    } else if #[cfg(feature="rt-tokio")] {
32                        tokio::time::sleep(Duration::from_millis(u64::from(millis))).await;
33                    }
34                }
35            }
36        }
37    }
38}