-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathexecutor.rs
75 lines (60 loc) · 1.42 KB
/
executor.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::future::Future;
use fusio::MaybeSend;
pub trait Executor {
fn spawn<F>(&self, future: F)
where
F: Future<Output = ()> + MaybeSend + 'static;
}
#[cfg(feature = "tokio")]
pub mod tokio {
use std::future::Future;
use fusio::MaybeSend;
use tokio::runtime::Handle;
use super::Executor;
#[derive(Debug, Clone)]
pub struct TokioExecutor {
handle: Handle,
}
impl TokioExecutor {
pub fn current() -> Self {
Self {
handle: Handle::current(),
}
}
}
impl Executor for TokioExecutor {
fn spawn<F>(&self, future: F)
where
F: Future<Output = ()> + MaybeSend + 'static,
{
self.handle.spawn(future);
}
}
}
#[cfg(all(feature = "opfs", target_arch = "wasm32"))]
pub mod opfs {
use std::future::Future;
use fusio::MaybeSend;
use wasm_bindgen::prelude::*;
use super::Executor;
#[wasm_bindgen]
pub struct OpfsExecutor();
impl Default for OpfsExecutor {
fn default() -> Self {
Self {}
}
}
impl OpfsExecutor {
pub fn new() -> Self {
Self {}
}
}
impl Executor for OpfsExecutor {
fn spawn<F>(&self, future: F)
where
F: Future<Output = ()> + MaybeSend + 'static,
{
wasm_bindgen_futures::spawn_local(future);
}
}
}