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        } else {
23            mod singlethread;
24            pub(crate) use singlethread::*;
25         }
26    }
27}
28
29/// Spawns a [`Future<Output = T>`](core::future::Future) that can execute on
30/// any thread; returns a [`Task`].
31///
32/// The future will be polled to completion in the background. Awaiting the
33/// returned task has no effect when the future is polled. Dropping the task
34/// will cancel the future, unless you call [`Task::detach()`] first.
35#[inline]
36pub fn spawn<F>(future: F) -> Task<F::Output>
37where
38    F: Future + Send + 'static,
39    F::Output: Send + 'static,
40{
41    runtime::Job::spawn(future)
42}
43
44/// Spawns a [`Future<Output = T>`](core::future::Future) that executes on the
45/// current thread; returns a [`Task`].
46///
47/// The future will be polled to completion in the background. Awaiting the
48/// returned task has no effect when the future is polled. Dropping the task
49/// will cancel the future, unless you call [`Task::detach()`] first.
50#[inline]
51pub fn spawn_local<F>(future: F) -> Task<F::Output>
52where
53    F: Future + 'static,
54    F::Output: 'static,
55{
56    runtime::Job::spawn_local(future)
57}