web_task/lib.rs
1use async_task::{Runnable, Task};
2use queue::Queue;
3
4extern crate alloc;
5
6mod queue;
7
8/// Spawns a task that is executed by the browser event loop.
9pub fn spawn_local<F>(future: F) -> Task<F::Output>
10where
11 F: Future + 'static,
12 F::Output: 'static,
13{
14 spawn_with(future, Queue::schedule)
15}
16
17fn spawn_with<F, S>(future: F, schedule: S) -> Task<F::Output>
18where
19 F: Future + 'static,
20 F::Output: 'static,
21 S: Fn(Runnable) + Send + Sync + 'static,
22{
23 // SAFETY: of the four `spawn_unchecked` requirements:
24 //
25 // + `future` is not `Send`, so we must show the runnable is only used and
26 // dropped on the same thread. This crate assumes single-threaded
27 // operation, so we assert this is the case.
28 //
29 // + `future` is `'static`.
30 //
31 // + `schedule` is `Send` and `Sync`.
32 //
33 // + `schedule` is `'static`.
34 //
35 let (runnable, task) = unsafe { async_task::spawn_unchecked(future, &schedule) };
36 schedule(runnable);
37 task
38}