async_deferred/
spawner.rs1use core::future::Future;
2
3use crate::SpawnError;
4
5pub trait Spawner {
37 fn spawn<F>(&self, task: F) -> Result<(), SpawnError>
39 where
40 F: Future<Output = ()> + Send + 'static;
41}
42
43pub trait LocalSpawner {
52 fn spawn_local<F>(&self, task: F) -> Result<(), SpawnError>
54 where
55 F: Future<Output = ()> + 'static;
56}
57
58macro_rules! forward_spawner {
61 ($($(#[$attr:meta])* $pointer:ty),* $(,)?) => {$(
62 $(#[$attr])*
63 impl<S: Spawner + ?Sized> Spawner for $pointer {
64 fn spawn<F>(&self, task: F) -> Result<(), SpawnError>
65 where
66 F: Future<Output = ()> + Send + 'static,
67 {
68 (**self).spawn(task)
69 }
70 }
71 )*};
72}
73
74macro_rules! forward_local_spawner {
76 ($($(#[$attr:meta])* $pointer:ty),* $(,)?) => {$(
77 $(#[$attr])*
78 impl<S: LocalSpawner + ?Sized> LocalSpawner for $pointer {
79 fn spawn_local<F>(&self, task: F) -> Result<(), SpawnError>
80 where
81 F: Future<Output = ()> + 'static,
82 {
83 (**self).spawn_local(task)
84 }
85 }
86 )*};
87}
88
89forward_spawner!(
90 &S,
91 &mut S,
92 #[cfg(feature = "alloc")]
93 alloc::boxed::Box<S>,
94 #[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
95 alloc::sync::Arc<S>,
96);
97forward_local_spawner!(
98 &S,
99 &mut S,
100 #[cfg(feature = "alloc")]
101 alloc::boxed::Box<S>,
102 #[cfg(feature = "alloc")]
103 alloc::rc::Rc<S>,
104 #[cfg(all(feature = "alloc", target_has_atomic = "ptr"))]
105 alloc::sync::Arc<S>,
106);
107
108#[cfg(feature = "tokio")]
113#[derive(Debug, Clone, Copy, Default)]
114pub struct Tokio;
115
116#[cfg(feature = "tokio")]
117impl Spawner for Tokio {
118 fn spawn<F>(&self, task: F) -> Result<(), SpawnError>
119 where
120 F: Future<Output = ()> + Send + 'static,
121 {
122 let handle = tokio::runtime::Handle::try_current()
123 .map_err(|_| SpawnError::new("not inside a Tokio runtime"))?;
124 handle.spawn(task);
125 Ok(())
126 }
127}
128
129#[cfg(feature = "tokio")]
131impl Spawner for tokio::runtime::Handle {
132 fn spawn<F>(&self, task: F) -> Result<(), SpawnError>
133 where
134 F: Future<Output = ()> + Send + 'static,
135 {
136 tokio::runtime::Handle::spawn(self, task);
137 Ok(())
138 }
139}
140
141#[cfg(feature = "tokio")]
143impl LocalSpawner for tokio::task::LocalSet {
144 fn spawn_local<F>(&self, task: F) -> Result<(), SpawnError>
145 where
146 F: Future<Output = ()> + 'static,
147 {
148 tokio::task::LocalSet::spawn_local(self, task);
149 Ok(())
150 }
151}
152
153#[cfg(feature = "smol")]
155#[derive(Debug, Clone, Copy, Default)]
156pub struct Smol;
157
158#[cfg(feature = "smol")]
159impl Spawner for Smol {
160 fn spawn<F>(&self, task: F) -> Result<(), SpawnError>
161 where
162 F: Future<Output = ()> + Send + 'static,
163 {
164 smol::spawn(task).detach();
165 Ok(())
166 }
167}
168
169#[cfg(feature = "smol")]
171impl<'a> Spawner for smol::Executor<'a> {
172 fn spawn<F>(&self, task: F) -> Result<(), SpawnError>
173 where
174 F: Future<Output = ()> + Send + 'static,
175 {
176 smol::Executor::spawn(self, task).detach();
177 Ok(())
178 }
179}
180
181#[cfg(feature = "smol")]
184impl<'a> LocalSpawner for smol::LocalExecutor<'a> {
185 fn spawn_local<F>(&self, task: F) -> Result<(), SpawnError>
186 where
187 F: Future<Output = ()> + 'static,
188 {
189 smol::LocalExecutor::spawn(self, task).detach();
190 Ok(())
191 }
192}