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
157
158
159
160
161
162
163
164
165
166
167
use core::future::Future;

/// A spawner trait for spawning futures.
pub trait AsyncLocalSpawner: Copy + 'static {
  /// The handle returned by the spawner when a future is spawned.
  type JoinHandle<F>: Future + 'static
  where
    F: 'static;

  /// Spawn a future.
  fn spawn<F>(future: F) -> Self::JoinHandle<F::Output>
  where
    F::Output: 'static,
    F: Future + 'static;

  /// Spawn a future and detach it.
  fn spawn_detach<F>(future: F)
  where
    F::Output: 'static,
    F: Future + 'static,
  {
    core::mem::drop(Self::spawn(future));
  }
}

/// A [`AsyncLocalSpawner`] that uses the [`tokio`] runtime.
#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
#[derive(Debug, Clone, Copy)]
pub struct TokioLocalSpawner;

#[cfg(feature = "tokio")]
impl AsyncLocalSpawner for TokioLocalSpawner {
  type JoinHandle<F> = tokio::task::JoinHandle<F> where
  F: 'static;

  fn spawn<F>(future: F) -> Self::JoinHandle<F::Output>
  where
    F::Output: 'static,
    F: core::future::Future + 'static,
  {
    tokio::task::spawn_local(future)
  }
}

/// A [`AsyncLocalSpawner`] that uses the [`async-std`](async_std) runtime.
#[cfg(feature = "async-std")]
#[cfg_attr(docsrs, doc(cfg(feature = "async-std")))]
#[derive(Debug, Clone, Copy)]
pub struct AsyncStdLocalSpawner;

#[cfg(feature = "async-std")]
impl AsyncLocalSpawner for AsyncStdLocalSpawner {
  type JoinHandle<F> = async_std::task::JoinHandle<F> where F: 'static;

  fn spawn<F>(future: F) -> Self::JoinHandle<F::Output>
  where
    F::Output: 'static,
    F: core::future::Future + 'static,
  {
    async_std::task::spawn_local(future)
  }
}

/// A [`AsyncLocalSpawner`] that uses the [`smol`] runtime.
#[cfg(feature = "smol")]
#[cfg_attr(docsrs, doc(cfg(feature = "smol")))]
#[derive(Debug, Clone, Copy)]
pub struct SmolLocalSpawner;

#[cfg(feature = "smol")]
impl AsyncLocalSpawner for SmolLocalSpawner {
  type JoinHandle<F> = smol::Task<F> where F: 'static;

  fn spawn<F>(future: F) -> Self::JoinHandle<F::Output>
  where
    F::Output: 'static,
    F: core::future::Future + 'static,
  {
    smol::LocalExecutor::new().spawn(future)
  }

  fn spawn_detach<F>(future: F)
  where
    F::Output: 'static,
    F: Future + 'static,
  {
    smol::LocalExecutor::new().spawn(future).detach();
  }
}

/// The join handle returned by [`WasmLocalSpawner`].
#[cfg(feature = "wasm")]
#[cfg_attr(docsrs, doc(cfg(feature = "wasm")))]
pub struct WasmLocalJoinHandle<F> {
  stop_tx: futures_channel::oneshot::Sender<bool>,
  rx: futures_channel::oneshot::Receiver<F>,
}

#[cfg(feature = "wasm")]
impl<F> Future for WasmLocalJoinHandle<F> {
  type Output = Result<F, futures_channel::oneshot::Canceled>;

  fn poll(
    mut self: core::pin::Pin<&mut Self>,
    cx: &mut core::task::Context<'_>,
  ) -> core::task::Poll<Self::Output> {
    core::pin::Pin::new(&mut self.rx).poll(cx)
  }
}

#[cfg(feature = "wasm")]
impl<F> WasmLocalJoinHandle<F> {
  /// Detach the future from the spawner.
  #[inline]
  pub fn detach(self) {
    let _ = self.stop_tx.send(false);
  }

  /// Cancel the future.
  #[inline]
  pub fn cancel(self) {
    let _ = self.stop_tx.send(true);
  }
}

/// A [`AsyncLocalSpawner`] that uses the [`wasm-bindgen-futures`](wasm_bindgen_futures) runtime.
#[cfg(feature = "wasm")]
#[cfg_attr(docsrs, doc(cfg(feature = "wasm")))]
#[derive(Debug, Clone, Copy)]
pub struct WasmLocalSpawner;

#[cfg(feature = "wasm")]
impl AsyncLocalSpawner for WasmLocalSpawner {
  type JoinHandle<F> = WasmLocalJoinHandle<F> where F: 'static;

  fn spawn<F>(future: F) -> Self::JoinHandle<F::Output>
  where
    F::Output: 'static,
    F: core::future::Future + 'static,
  {
    use futures_util::FutureExt;

    let (tx, rx) = futures_channel::oneshot::channel();
    let (stop_tx, stop_rx) = futures_channel::oneshot::channel();
    wasm_bindgen_futures::spawn_local(async {
      futures_util::pin_mut!(future);

      futures_util::select! {
        sig = stop_rx.fuse() => {
          match sig {
            Ok(true) => {
              // if we receive a stop signal, we just stop this task.
            },
            Ok(false) | Err(_) => {
              let _ = future.await;
            },
          }
        },
        future = (&mut future).fuse() => {
          let _ = tx.send(future);
        }
      }
    });
    WasmLocalJoinHandle { stop_tx, rx }
  }
}