|
1 | 1 | use super::TaskPool;
|
2 | 2 | use std::{ops::Deref, sync::OnceLock};
|
3 | 3 |
|
4 |
| -static COMPUTE_TASK_POOL: OnceLock<ComputeTaskPool> = OnceLock::new(); |
5 |
| -static ASYNC_COMPUTE_TASK_POOL: OnceLock<AsyncComputeTaskPool> = OnceLock::new(); |
6 |
| -static IO_TASK_POOL: OnceLock<IoTaskPool> = OnceLock::new(); |
7 |
| - |
8 |
| -/// A newtype for a task pool for CPU-intensive work that must be completed to |
9 |
| -/// deliver the next frame |
10 |
| -/// |
11 |
| -/// See [`TaskPool`] documentation for details on Bevy tasks. |
12 |
| -/// [`AsyncComputeTaskPool`] should be preferred if the work does not have to be |
13 |
| -/// completed before the next frame. |
14 |
| -#[derive(Debug)] |
15 |
| -pub struct ComputeTaskPool(TaskPool); |
16 |
| - |
17 |
| -impl ComputeTaskPool { |
18 |
| - /// Initializes the global [`ComputeTaskPool`] instance. |
19 |
| - pub fn init(f: impl FnOnce() -> TaskPool) -> &'static Self { |
20 |
| - COMPUTE_TASK_POOL.get_or_init(|| Self(f())) |
21 |
| - } |
22 |
| - |
23 |
| - /// Gets the global [`ComputeTaskPool`] instance. |
24 |
| - /// |
25 |
| - /// # Panics |
26 |
| - /// Panics if no pool has been initialized yet. |
27 |
| - pub fn get() -> &'static Self { |
28 |
| - COMPUTE_TASK_POOL.get().expect( |
29 |
| - "A ComputeTaskPool has not been initialized yet. Please call \ |
30 |
| - ComputeTaskPool::init beforehand.", |
31 |
| - ) |
32 |
| - } |
| 4 | +macro_rules! taskpool { |
| 5 | + ($(#[$attr:meta])* ($static:ident, $type:ident)) => { |
| 6 | + static $static: OnceLock<$type> = OnceLock::new(); |
| 7 | + |
| 8 | + $(#[$attr])* |
| 9 | + #[derive(Debug)] |
| 10 | + pub struct $type(TaskPool); |
| 11 | + |
| 12 | + impl $type { |
| 13 | + #[doc = concat!(" Gets the global [`", stringify!($type), "`] instance, or initializes it with `f`.")] |
| 14 | + pub fn get_or_init(f: impl FnOnce() -> TaskPool) -> &'static Self { |
| 15 | + $static.get_or_init(|| Self(f())) |
| 16 | + } |
| 17 | + |
| 18 | + #[doc = concat!(" Attempts to get the global [`", stringify!($type), "`] instance, \ |
| 19 | + or returns `None` if it is not initialized.")] |
| 20 | + pub fn try_get() -> Option<&'static Self> { |
| 21 | + $static.get() |
| 22 | + } |
| 23 | + |
| 24 | + #[doc = concat!(" Gets the global [`", stringify!($type), "`] instance.")] |
| 25 | + #[doc = ""] |
| 26 | + #[doc = " # Panics"] |
| 27 | + #[doc = " Panics if the global instance has not been initialized yet."] |
| 28 | + pub fn get() -> &'static Self { |
| 29 | + $static.get().expect( |
| 30 | + concat!( |
| 31 | + "The ", |
| 32 | + stringify!($type), |
| 33 | + " has not been initialized yet. Please call ", |
| 34 | + stringify!($type), |
| 35 | + "::get_or_init beforehand." |
| 36 | + ) |
| 37 | + ) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + impl Deref for $type { |
| 42 | + type Target = TaskPool; |
| 43 | + |
| 44 | + fn deref(&self) -> &Self::Target { |
| 45 | + &self.0 |
| 46 | + } |
| 47 | + } |
| 48 | + }; |
33 | 49 | }
|
34 | 50 |
|
35 |
| -impl Deref for ComputeTaskPool { |
36 |
| - type Target = TaskPool; |
37 |
| - |
38 |
| - fn deref(&self) -> &Self::Target { |
39 |
| - &self.0 |
40 |
| - } |
41 |
| -} |
42 |
| - |
43 |
| -/// A newtype for a task pool for CPU-intensive work that may span across multiple frames |
44 |
| -/// |
45 |
| -/// See [`TaskPool`] documentation for details on Bevy tasks. Use [`ComputeTaskPool`] if |
46 |
| -/// the work must be complete before advancing to the next frame. |
47 |
| -#[derive(Debug)] |
48 |
| -pub struct AsyncComputeTaskPool(TaskPool); |
49 |
| - |
50 |
| -impl AsyncComputeTaskPool { |
51 |
| - /// Initializes the global [`AsyncComputeTaskPool`] instance. |
52 |
| - pub fn init(f: impl FnOnce() -> TaskPool) -> &'static Self { |
53 |
| - ASYNC_COMPUTE_TASK_POOL.get_or_init(|| Self(f())) |
54 |
| - } |
55 |
| - |
56 |
| - /// Gets the global [`AsyncComputeTaskPool`] instance. |
| 51 | +taskpool! { |
| 52 | + /// A newtype for a task pool for CPU-intensive work that must be completed to |
| 53 | + /// deliver the next frame |
57 | 54 | ///
|
58 |
| - /// # Panics |
59 |
| - /// Panics if no pool has been initialized yet. |
60 |
| - pub fn get() -> &'static Self { |
61 |
| - ASYNC_COMPUTE_TASK_POOL.get().expect( |
62 |
| - "A AsyncComputeTaskPool has not been initialized yet. Please call \ |
63 |
| - AsyncComputeTaskPool::init beforehand.", |
64 |
| - ) |
65 |
| - } |
66 |
| -} |
67 |
| - |
68 |
| -impl Deref for AsyncComputeTaskPool { |
69 |
| - type Target = TaskPool; |
70 |
| - |
71 |
| - fn deref(&self) -> &Self::Target { |
72 |
| - &self.0 |
73 |
| - } |
| 55 | + /// See [`TaskPool`] documentation for details on Bevy tasks. |
| 56 | + /// [`AsyncComputeTaskPool`] should be preferred if the work does not have to be |
| 57 | + /// completed before the next frame. |
| 58 | + (COMPUTE_TASK_POOL, ComputeTaskPool) |
74 | 59 | }
|
75 | 60 |
|
76 |
| -/// A newtype for a task pool for IO-intensive work (i.e. tasks that spend very little time in a |
77 |
| -/// "woken" state) |
78 |
| -#[derive(Debug)] |
79 |
| -pub struct IoTaskPool(TaskPool); |
80 |
| - |
81 |
| -impl IoTaskPool { |
82 |
| - /// Initializes the global [`IoTaskPool`] instance. |
83 |
| - pub fn init(f: impl FnOnce() -> TaskPool) -> &'static Self { |
84 |
| - IO_TASK_POOL.get_or_init(|| Self(f())) |
85 |
| - } |
86 |
| - |
87 |
| - /// Gets the global [`IoTaskPool`] instance. |
| 61 | +taskpool! { |
| 62 | + /// A newtype for a task pool for CPU-intensive work that may span across multiple frames |
88 | 63 | ///
|
89 |
| - /// # Panics |
90 |
| - /// Panics if no pool has been initialized yet. |
91 |
| - pub fn get() -> &'static Self { |
92 |
| - IO_TASK_POOL.get().expect( |
93 |
| - "A IoTaskPool has not been initialized yet. Please call \ |
94 |
| - IoTaskPool::init beforehand.", |
95 |
| - ) |
96 |
| - } |
| 64 | + /// See [`TaskPool`] documentation for details on Bevy tasks. |
| 65 | + /// Use [`ComputeTaskPool`] if the work must be complete before advancing to the next frame. |
| 66 | + (ASYNC_COMPUTE_TASK_POOL, AsyncComputeTaskPool) |
97 | 67 | }
|
98 | 68 |
|
99 |
| -impl Deref for IoTaskPool { |
100 |
| - type Target = TaskPool; |
101 |
| - |
102 |
| - fn deref(&self) -> &Self::Target { |
103 |
| - &self.0 |
104 |
| - } |
| 69 | +taskpool! { |
| 70 | + /// A newtype for a task pool for IO-intensive work (i.e. tasks that spend very little time in a |
| 71 | + /// "woken" state) |
| 72 | + /// |
| 73 | + /// See [`TaskPool`] documentation for details on Bevy tasks. |
| 74 | + (IO_TASK_POOL, IoTaskPool) |
105 | 75 | }
|
106 | 76 |
|
107 | 77 | /// A function used by `bevy_core` to tick the global tasks pools on the main thread.
|
|
0 commit comments