Skip to main content

web_task/
lib.rs

1#![doc = include_str!("../README.md")]
2#![no_std]
3#![cfg_attr(
4    target_feature = "atomics",
5    feature(thread_local, stdarch_wasm_atomic_wait)
6)]
7#![deny(missing_docs)]
8
9extern crate alloc;
10
11use async_task::Task;
12
13mod queue;
14
15mod runtime {
16    use cfg_if::cfg_if;
17
18    cfg_if! {
19        if #[cfg(target_feature = "atomics")] {
20            mod multithread;
21            pub(crate) use multithread::*;
22
23        } else {
24            mod singlethread;
25            pub(crate) use singlethread::*;
26         }
27    }
28}
29
30/// Spawns a [`Future<Output = T>`](core::future::Future) that can execute on
31/// any thread; returns a [`Task`].
32///
33/// The future will be polled to completion in the background. Awaiting the
34/// returned task has no effect when the future is polled. Dropping the task
35/// will cancel the future, unless you call [`Task::detach()`] first.
36#[inline]
37pub fn spawn<F>(future: F) -> Task<F::Output>
38where
39    F: Future + Send + 'static,
40    F::Output: Send + 'static,
41{
42    runtime::Job::spawn(future)
43}
44
45/// Spawns a [`Future<Output = T>`](core::future::Future) that executes on the
46/// current thread; returns a [`Task`].
47///
48/// The future will be polled to completion in the background. Awaiting the
49/// returned task has no effect when the future is polled. Dropping the task
50/// will cancel the future, unless you call [`Task::detach()`] first.
51#[inline]
52pub fn spawn_local<F>(future: F) -> Task<F::Output>
53where
54    F: Future + 'static,
55    F::Output: 'static,
56{
57    runtime::Job::spawn_local(future)
58}