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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use std::future::Future;
#[cfg(feature = "with-async_std-1")]
pub use async_std_impl::*;
#[cfg(feature = "with-smol-1")]
pub use smol_impl::*;
#[cfg(feature = "with-tokio-1")]
pub use tokio_impl::*;
#[cfg(feature = "with-wasm_bindgen-0_2")]
pub use wasm_bindgen_impl::*;
use crate::{Actor, ActorManager, Address};
pub trait Spawner {
fn spawn<F: Future<Output = ()> + Send + 'static>(&mut self, fut: F);
}
#[cfg(feature = "with-async_std-1")]
mod async_std_impl {
use super::*;
#[derive(Copy, Clone, Debug, Default)]
pub struct AsyncStd;
impl Spawner for AsyncStd {
fn spawn<F: Future<Output = ()> + Send + 'static>(&mut self, fut: F) {
async_std::task::spawn(fut);
}
}
pub trait AsyncStdGlobalSpawnExt<A: Actor> {
fn spawn_global(self) -> Address<A>;
}
impl<A: Actor> AsyncStdGlobalSpawnExt<A> for ActorManager<A> {
fn spawn_global(self) -> Address<A> {
self.spawn(&mut AsyncStd)
}
}
}
#[cfg(feature = "with-smol-1")]
mod smol_impl {
use super::*;
#[derive(Copy, Clone, Debug)]
pub enum Smol<'a> {
Global,
Handle(&'a smol::Executor<'a>),
}
impl<'a> Default for Smol<'a> {
fn default() -> Self {
Smol::Global
}
}
impl<'a> Spawner for Smol<'a> {
fn spawn<F: Future<Output = ()> + Send + 'static>(&mut self, fut: F) {
let task = match self {
Smol::Global => smol::spawn(fut),
Smol::Handle(e) => e.spawn(fut),
};
task.detach();
}
}
pub trait SmolGlobalSpawnExt<A: Actor> {
fn spawn_global(self) -> Address<A>;
}
impl<A: Actor> SmolGlobalSpawnExt<A> for ActorManager<A> {
fn spawn_global(self) -> Address<A> {
self.spawn(&mut Smol::Global)
}
}
}
#[cfg(feature = "with-tokio-1")]
mod tokio_impl {
use super::*;
#[derive(Copy, Clone, Debug)]
pub enum Tokio<'a> {
Global,
Handle(&'a tokio::runtime::Runtime),
}
impl<'a> Default for Tokio<'a> {
fn default() -> Self {
Tokio::Global
}
}
impl<'a> Spawner for Tokio<'a> {
fn spawn<F: Future<Output = ()> + Send + 'static>(&mut self, fut: F) {
match self {
Tokio::Global => tokio::spawn(fut),
Tokio::Handle(handle) => handle.spawn(fut),
};
}
}
pub trait TokioGlobalSpawnExt<A: Actor> {
fn spawn_global(self) -> Address<A>;
}
impl<A: Actor> TokioGlobalSpawnExt<A> for ActorManager<A> {
fn spawn_global(self) -> Address<A> {
self.spawn(&mut Tokio::Global)
}
}
}
#[cfg(feature = "with-wasm_bindgen-0_2")]
mod wasm_bindgen_impl {
use super::*;
#[derive(Copy, Clone, Debug, Default)]
pub struct WasmBindgen;
impl Spawner for WasmBindgen {
fn spawn<F: Future<Output = ()> + Send + 'static>(&mut self, fut: F) {
wasm_bindgen_futures::spawn_local(fut)
}
}
pub trait WasmBindgenGlobalSpawnExt<A: Actor> {
fn spawn_global(self) -> Address<A>;
}
impl<A: Actor> WasmBindgenGlobalSpawnExt<A> for ActorManager<A> {
fn spawn_global(self) -> Address<A> {
self.spawn(&mut WasmBindgen)
}
}
}