async_deferred/spawner.rs
1use core::future::Future;
2
3/// Runs a task in the background on an async runtime.
4///
5/// Implement this to use [`Deferred`](crate::Deferred) with any runtime. Implementations
6/// for Tokio and smol are available behind the `tokio` (default) and `smol` features.
7///
8/// The runtime must keep polling `task` until it finishes. If the runtime stops first,
9/// it must drop `task`, so the `Deferred` reports [`State::Cancelled`](crate::State::Cancelled)
10/// instead of staying pending.
11///
12/// # Examples
13///
14/// ```rust
15/// use std::future::Future;
16/// use async_deferred::{Deferred, Spawner};
17///
18/// struct ThreadSpawner;
19///
20/// impl Spawner for ThreadSpawner {
21/// fn spawn<F>(&self, task: F)
22/// where
23/// F: Future<Output = ()> + Send + 'static,
24/// {
25/// std::thread::spawn(move || futures_executor::block_on(task));
26/// }
27/// }
28///
29/// let mut deferred = Deferred::start_on(&ThreadSpawner, async { 42 });
30/// assert_eq!(futures_executor::block_on(deferred.join()), Ok(&42));
31/// ```
32pub trait Spawner {
33 /// Runs `task` in the background.
34 fn spawn<F>(&self, task: F)
35 where
36 F: Future<Output = ()> + Send + 'static;
37}
38
39/// Runs a task that is not `Send` in the background, on a single-threaded executor.
40///
41/// Use it with the `*_local_on` methods of [`Deferred`](crate::Deferred). The same
42/// requirements as [`Spawner`] apply.
43///
44/// # Embassy
45///
46/// Use [`embassy_spawner!`](crate::embassy_spawner) to declare a `LocalSpawner` for embassy.
47pub trait LocalSpawner {
48 /// Runs `task` in the background.
49 fn spawn_local<F>(&self, task: F)
50 where
51 F: Future<Output = ()> + 'static;
52}
53
54/// Spawns on the current Tokio runtime.
55///
56/// Spawning panics outside a Tokio runtime. Use a [`tokio::runtime::Handle`] there.
57#[cfg(feature = "tokio")]
58#[derive(Debug, Clone, Copy, Default)]
59pub struct Tokio;
60
61#[cfg(feature = "tokio")]
62impl Spawner for Tokio {
63 fn spawn<F>(&self, task: F)
64 where
65 F: Future<Output = ()> + Send + 'static,
66 {
67 tokio::spawn(task);
68 }
69}
70
71/// Spawns on this Tokio runtime. Works from outside the runtime.
72#[cfg(feature = "tokio")]
73impl Spawner for tokio::runtime::Handle {
74 fn spawn<F>(&self, task: F)
75 where
76 F: Future<Output = ()> + Send + 'static,
77 {
78 tokio::runtime::Handle::spawn(self, task);
79 }
80}
81
82/// Spawns on smol's global executor.
83#[cfg(feature = "smol")]
84#[derive(Debug, Clone, Copy, Default)]
85pub struct Smol;
86
87#[cfg(feature = "smol")]
88impl Spawner for Smol {
89 fn spawn<F>(&self, task: F)
90 where
91 F: Future<Output = ()> + Send + 'static,
92 {
93 smol::spawn(task).detach();
94 }
95}
96
97/// Spawns on this smol executor. The task only makes progress while the executor is run.
98#[cfg(feature = "smol")]
99impl Spawner for smol::Executor<'static> {
100 fn spawn<F>(&self, task: F)
101 where
102 F: Future<Output = ()> + Send + 'static,
103 {
104 smol::Executor::spawn(self, task).detach();
105 }
106}