agnostic_lite/
spawner.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
use core::future::Future;

use crate::Yielder;

#[cfg(any(feature = "smol", feature = "async-std"))]
macro_rules! join_handle {
  ($handle:ty) => {
    pin_project_lite::pin_project! {
      /// An owned permission to join on a task (await its termination).
      pub struct JoinHandle<T> {
        #[pin]
        handle: $handle,
      }
    }

    impl<T> From<$handle> for JoinHandle<T> {
      fn from(handle: $handle) -> Self {
        Self { handle }
      }
    }

    impl<T> core::future::Future for JoinHandle<T> {
      type Output = core::result::Result<T, $crate::spawner::handle::JoinError>;

      fn poll(
        self: core::pin::Pin<&mut Self>,
        cx: &mut core::task::Context<'_>,
      ) -> core::task::Poll<Self::Output> {
        let this = self.project();

        match this.handle.poll(cx) {
          core::task::Poll::Ready(v) => core::task::Poll::Ready(Ok(v)),
          core::task::Poll::Pending => core::task::Poll::Pending,
        }
      }
    }
  };
}

pub(crate) mod handle {
  /// Task failed to execute to completion.
  ///
  /// This error will never be returned for `smol` and `async-std` runtime,
  /// having it here is just for compatibility with other runtimes.
  #[derive(Debug, Clone, PartialEq, Eq)]
  pub struct JoinError(());

  impl JoinError {
    /// Create a new `JoinError`.
    #[inline]
    #[cfg(feature = "wasm")]
    pub(crate) const fn new() -> Self {
      Self(())
    }
  }

  impl core::fmt::Display for JoinError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
      write!(f, "task failed to execute to completion")
    }
  }

  impl core::error::Error for JoinError {}

  #[cfg(feature = "std")]
  impl From<JoinError> for std::io::Error {
    fn from(_: JoinError) -> Self {
      std::io::Error::new(std::io::ErrorKind::Other, "join error")
    }
  }
}

/// Joinhanlde trait
pub trait JoinHandle<O>: Future<Output = Result<O, Self::JoinError>> + Unpin {
  /// The error type for the join handle
  #[cfg(feature = "std")]
  type JoinError: core::error::Error + Into<std::io::Error> + Send + Sync + 'static;

  /// The error type for the join handle
  #[cfg(not(feature = "std"))]
  type JoinError: core::error::Error + Send + Sync + 'static;

  /// Detaches the task to let it keep running in the background.
  fn detach(self)
  where
    Self: Sized,
  {
    drop(self)
  }
}

/// Joinhanlde trait
pub trait LocalJoinHandle<O>: Future<Output = Result<O, Self::JoinError>> + Unpin {
  /// The error type for the join handle
  #[cfg(feature = "std")]
  type JoinError: core::error::Error + Into<std::io::Error> + 'static;
  /// The error type for the join handle
  #[cfg(not(feature = "std"))]
  type JoinError: core::error::Error + 'static;

  /// Detaches the task to let it keep running in the background.
  fn detach(self)
  where
    Self: Sized,
  {
    drop(self)
  }
}

/// A spawner trait for spawning futures.
pub trait AsyncSpawner: Yielder + Copy + Send + Sync + 'static {
  /// The handle returned by the spawner when a future is spawned.
  type JoinHandle<O>: JoinHandle<O> + Send + Sync + 'static
  where
    O: Send + 'static;

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

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

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

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

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

/// A spawner trait for spawning blocking.
pub trait AsyncBlockingSpawner: Yielder + Copy + 'static {
  /// The join handle type for blocking tasks
  type JoinHandle<R>: JoinHandle<R> + Send + 'static
  where
    R: Send + 'static;

  /// Spawn a blocking function onto the runtime
  fn spawn_blocking<F, R>(f: F) -> Self::JoinHandle<R>
  where
    F: FnOnce() -> R + Send + 'static,
    R: Send + 'static;

  /// Spawn a blocking function onto the runtime and detach it
  fn spawn_blocking_detach<F, R>(f: F)
  where
    F: FnOnce() -> R + Send + 'static,
    R: Send + 'static,
  {
    Self::spawn_blocking(f).detach();
  }
}

/// Canceled
#[derive(Debug, Clone, Copy)]
#[cfg(all(
  feature = "time",
  any(
    feature = "async-std",
    feature = "tokio",
    feature = "smol",
    feature = "wasm"
  )
))]
pub(crate) struct Canceled;

#[cfg(all(
  feature = "time",
  any(
    feature = "async-std",
    feature = "tokio",
    feature = "smol",
    feature = "wasm"
  )
))]
impl core::fmt::Display for Canceled {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    write!(f, "after canceled")
  }
}

#[cfg(all(
  feature = "time",
  any(
    feature = "async-std",
    feature = "tokio",
    feature = "smol",
    feature = "wasm"
  )
))]
impl core::error::Error for Canceled {}

/// Error of [`AfterHandle`]'s output
#[cfg(feature = "time")]
#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
#[derive(Debug)]
pub enum AfterHandleError<E> {
  /// The after function was canceled
  Canceled,
  /// Task failed to execute to completion.
  Join(E),
}

#[cfg(feature = "time")]
impl<E: core::fmt::Display> core::fmt::Display for AfterHandleError<E> {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    match self {
      Self::Canceled => write!(f, "after function was canceled"),
      Self::Join(e) => write!(f, "{e}"),
    }
  }
}

#[cfg(feature = "time")]
impl<E: core::error::Error> core::error::Error for AfterHandleError<E> {}

#[cfg(feature = "time")]
impl<E: core::error::Error + Send + Sync + 'static> From<AfterHandleError<E>> for std::io::Error {
  fn from(value: AfterHandleError<E>) -> Self {
    match value {
      AfterHandleError::Canceled => std::io::Error::new(std::io::ErrorKind::Other, "task canceled"),
      AfterHandleError::Join(e) => std::io::Error::new(std::io::ErrorKind::Other, e),
    }
  }
}

#[cfg(all(
  feature = "time",
  any(
    feature = "async-std",
    feature = "tokio",
    feature = "smol",
    feature = "wasm"
  )
))]
pub(crate) struct AfterHandleSignals {
  finished: core::sync::atomic::AtomicBool,
  expired: core::sync::atomic::AtomicBool,
}

#[cfg(all(
  feature = "time",
  any(
    feature = "async-std",
    feature = "tokio",
    feature = "smol",
    feature = "wasm"
  )
))]
impl AfterHandleSignals {
  #[inline]
  pub(crate) const fn new() -> Self {
    Self {
      finished: core::sync::atomic::AtomicBool::new(false),
      expired: core::sync::atomic::AtomicBool::new(false),
    }
  }

  #[inline]
  pub(crate) fn set_finished(&self) {
    self
      .finished
      .store(true, core::sync::atomic::Ordering::Release);
  }

  #[inline]
  pub(crate) fn set_expired(&self) {
    self
      .expired
      .store(true, core::sync::atomic::Ordering::Release);
  }

  #[inline]
  pub(crate) fn is_finished(&self) -> bool {
    self.finished.load(core::sync::atomic::Ordering::Acquire)
  }

  #[inline]
  pub(crate) fn is_expired(&self) -> bool {
    self.expired.load(core::sync::atomic::Ordering::Acquire)
  }
}

/// The handle returned by the [`AsyncAfterSpawner`] when a after future is spawned.
///
/// Drop the handle to detach the task.
#[cfg(feature = "time")]
#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
pub trait AfterHandle<F: Send + 'static>:
  Send + Sync + Future<Output = Result<F, Self::JoinError>> + 'static
{
  /// The join error type for the join handle
  type JoinError: core::error::Error + Into<std::io::Error> + Send + Sync + 'static;

  /// Cancels the task related to this handle.
  ///
  /// Returns the task’s output if it was completed just before it got canceled, or `None` if it didn’t complete.
  fn cancel(self) -> impl Future<Output = Option<Result<F, Self::JoinError>>> + Send;

  /// Resets the delay of the task related to this handle.
  fn reset(&self, duration: core::time::Duration);

  /// Aborts the task related to this handle.
  fn abort(self);

  /// Detaches the task to let it keep running in the background.
  fn detach(self)
  where
    Self: Sized,
  {
    drop(self)
  }

  /// Returns `true` if the timer has expired.
  fn is_expired(&self) -> bool;

  /// Returns `true` if the task has finished.
  fn is_finished(&self) -> bool;
}

/// A spawner trait for spawning futures. Go's `time.AfterFunc` equivalent.
#[cfg(feature = "time")]
#[cfg_attr(docsrs, doc(cfg(feature = "time")))]
pub trait AsyncAfterSpawner: Copy + Send + Sync + 'static {
  /// The handle returned by the spawner when a future is spawned.
  type JoinHandle<F>: AfterHandle<F>
  where
    F: Send + 'static;

  /// Spawn a future onto the runtime and run the given future after the given duration
  fn spawn_after<F>(duration: core::time::Duration, future: F) -> Self::JoinHandle<F::Output>
  where
    F::Output: Send + 'static,
    F: Future + Send + 'static;

  /// Spawn and detach a future onto the runtime and run the given future after the given duration
  fn spawn_after_detach<F>(duration: core::time::Duration, future: F)
  where
    F::Output: Send + 'static,
    F: Future + Send + 'static,
  {
    core::mem::drop(Self::spawn_after(duration, future));
  }

  /// Spawn a future onto the runtime and run the given future after reach the given instant
  fn spawn_after_at<F>(instant: std::time::Instant, future: F) -> Self::JoinHandle<F::Output>
  where
    F::Output: Send + 'static,
    F: Future + Send + 'static;

  /// Spawn and detach a future onto the runtime and run the given future after reach the given instant
  fn spawn_after_at_detach<F>(instant: std::time::Instant, future: F)
  where
    F::Output: Send + 'static,
    F: Future + Send + 'static,
  {
    Self::spawn_after_at(instant, future).detach()
  }
}