whatsapp_rust/
runtime_impl.rs1#[cfg(not(target_arch = "wasm32"))]
3mod tokio_impl {
4 use std::future::Future;
5 use std::pin::Pin;
6 use std::time::Duration;
7
8 use async_trait::async_trait;
9 use wacore::runtime::{AbortHandle, Runtime};
10
11 pub struct TokioRuntime;
12
13 #[async_trait]
14 impl Runtime for TokioRuntime {
15 fn spawn(&self, future: Pin<Box<dyn Future<Output = ()> + Send + 'static>>) -> AbortHandle {
16 let handle = tokio::spawn(future);
17 AbortHandle::new(move || handle.abort())
18 }
19
20 fn spawn_detached(&self, future: Pin<Box<dyn Future<Output = ()> + Send + 'static>>) {
21 tokio::spawn(future);
22 }
23
24 fn sleep(&self, duration: Duration) -> Pin<Box<dyn Future<Output = ()> + Send>> {
25 Box::pin(tokio::time::sleep(duration))
26 }
27
28 fn spawn_blocking(
29 &self,
30 f: Box<dyn FnOnce() + Send + 'static>,
31 ) -> Pin<Box<dyn Future<Output = ()> + Send>> {
32 Box::pin(async {
33 let _ = tokio::task::spawn_blocking(f).await;
34 })
35 }
36
37 fn yield_now(&self) -> Option<Pin<Box<dyn Future<Output = ()> + Send>>> {
38 None
41 }
42 }
43}
44
45#[cfg(not(target_arch = "wasm32"))]
46pub use tokio_impl::TokioRuntime;