bevy_impulse/async_execution/
mod.rs1use bevy_ecs::prelude::World;
19
20use std::future::Future;
21
22#[cfg(feature = "single_threaded_async")]
23mod single_threaded_execution;
24#[cfg(feature = "single_threaded_async")]
25pub(crate) use single_threaded_execution::SingleThreadedExecution;
26#[cfg(feature = "single_threaded_async")]
27use single_threaded_execution::SingleThreadedExecutionSender;
28
29pub(crate) use bevy_tasks::Task as TaskHandle;
30
31#[cfg(not(feature = "single_threaded_async"))]
32pub(crate) type CancelSender = AsyncComputeTaskPoolSender;
33
34#[cfg(feature = "single_threaded_async")]
35pub(crate) type CancelSender = SingleThreadedExecutionSender;
36
37pub(crate) fn spawn_task<T>(
38 future: impl Future<Output = T> + Sendish + 'static,
39 _world: &mut World,
40) -> TaskHandle<T>
41where
42 T: Send + 'static,
43{
44 #[cfg(not(feature = "single_threaded_async"))]
45 {
46 use bevy_tasks::AsyncComputeTaskPool;
47 AsyncComputeTaskPool::get().spawn(future)
48 }
49
50 #[cfg(feature = "single_threaded_async")]
51 {
52 SingleThreadedExecution::get(_world).spawn(future)
53 }
54}
55
56pub(crate) fn task_cancel_sender(_world: &mut World) -> CancelSender {
57 #[cfg(not(feature = "single_threaded_async"))]
58 {
59 AsyncComputeTaskPoolSender
60 }
61
62 #[cfg(feature = "single_threaded_async")]
63 {
64 SingleThreadedExecution::get(_world).cancel_sender()
65 }
66}
67
68#[cfg(not(feature = "single_threaded_async"))]
69pub(crate) struct AsyncComputeTaskPoolSender;
70
71#[cfg(not(feature = "single_threaded_async"))]
72impl AsyncComputeTaskPoolSender {
73 pub(crate) fn send<F>(&self, f: impl FnOnce() -> F)
76 where
77 F: Future + Send + 'static,
78 F::Output: Send + 'static,
79 {
80 use bevy_tasks::AsyncComputeTaskPool;
81 AsyncComputeTaskPool::get().spawn(f()).detach();
82 }
83}
84
85#[cfg(not(feature = "single_threaded_async"))]
90pub trait Sendish: Send {}
91
92#[cfg(not(feature = "single_threaded_async"))]
93impl<T: Send> Sendish for T {}
94
95#[cfg(feature = "single_threaded_async")]
100pub trait Sendish {}
101
102#[cfg(feature = "single_threaded_async")]
103impl<T> Sendish for T {}