async_rt/
task.rs

1use crate::global::GlobalExecutor;
2use crate::{
3    AbortableJoinHandle, CommunicationTask, Executor, JoinHandle, UnboundedCommunicationTask,
4};
5use futures::channel::mpsc::{Receiver, UnboundedReceiver};
6use std::future::Future;
7
8const EXECUTOR: GlobalExecutor = GlobalExecutor;
9
10/// Spawns a new asynchronous task in the background, returning an Future [`JoinHandle`] for it.
11pub fn spawn<F>(future: F) -> JoinHandle<F::Output>
12where
13    F: Future + Send + 'static,
14    F::Output: Send + 'static,
15{
16    EXECUTOR.spawn(future)
17}
18
19/// Spawns a new asynchronous task in the background, returning an abortable handle that will cancel the task
20/// once the handle is dropped.
21///
22/// Note: This function is used if the task is expected to run until the handle is dropped. It is recommended to use
23/// [`spawn`] or [`dispatch`] otherwise.
24pub fn spawn_abortable<F>(future: F) -> AbortableJoinHandle<F::Output>
25where
26    F: Future + Send + 'static,
27    F::Output: Send + 'static,
28{
29    EXECUTOR.spawn_abortable(future)
30}
31
32/// Spawns a new asynchronous task in the background without an handle.
33/// Basically the same as [`spawn`].
34pub fn dispatch<F>(future: F)
35where
36    F: Future + Send + 'static,
37    F::Output: Send + 'static,
38{
39    EXECUTOR.dispatch(future);
40}
41
42/// Spawns a new asynchronous task that accepts messages to the task using [`channels`](futures::channel::mpsc).
43/// This function returns an handle that allows sending a message or if there is no reference to the handle at all
44/// (in other words, all handles are dropped), the task would be aborted.
45pub fn spawn_coroutine<T, F, Fut>(f: F) -> CommunicationTask<T>
46where
47    F: FnMut(Receiver<T>) -> Fut,
48    Fut: Future<Output = ()> + Send + 'static,
49{
50    EXECUTOR.spawn_coroutine(f)
51}
52
53/// Spawns a new asynchronous task with provided context, that accepts messages to the task using [`channels`](futures::channel::mpsc).
54/// This function returns an handle that allows sending a message or if there is no reference to the handle at all
55/// (in other words, all handles are dropped), the task would be aborted.
56pub fn spawn_coroutine_with_context<T, F, C, Fut>(context: C, f: F) -> CommunicationTask<T>
57where
58    F: FnMut(C, Receiver<T>) -> Fut,
59    Fut: Future<Output = ()> + Send + 'static,
60{
61    EXECUTOR.spawn_coroutine_with_context(context, f)
62}
63
64/// Spawns a new asynchronous task that accepts messages to the task using [`channels`](futures::channel::mpsc).
65/// This function returns an handle that allows sending a message or if there is no reference to the handle at all
66/// (in other words, all handles are dropped), the task would be aborted.
67pub fn spawn_unbounded_coroutine<T, F, Fut>(f: F) -> UnboundedCommunicationTask<T>
68where
69    F: FnMut(UnboundedReceiver<T>) -> Fut,
70    Fut: Future<Output = ()> + Send + 'static,
71{
72    EXECUTOR.spawn_unbounded_coroutine(f)
73}
74
75/// Spawns a new asynchronous task with provided context, that accepts messages to the task using [`channels`](futures::channel::mpsc).
76/// This function returns an handle that allows sending a message or if there is no reference to the handle at all
77/// (in other words, all handles are dropped), the task would be aborted.
78pub fn spawn_unbounded_coroutine_with_context<T, F, C, Fut>(
79    context: C,
80    f: F,
81) -> UnboundedCommunicationTask<T>
82where
83    F: FnMut(C, UnboundedReceiver<T>) -> Fut,
84    Fut: Future<Output = ()> + Send + 'static,
85{
86    EXECUTOR.spawn_unbounded_coroutine_with_context(context, f)
87}