issun_core/mechanics/execution.rs
1//! Execution characteristics hints for mechanics.
2//!
3//! This module defines marker types that mechanics use to communicate their
4//! preferred execution model to ECS adapters (like issun-bevy).
5//!
6//! # Design Philosophy
7//!
8//! - **Hints, not requirements**: These are compile-time suggestions, not hard constraints
9//! - **Engine-agnostic**: issun-core declares preferences, adapters interpret them
10//! - **Zero-cost**: All types are zero-sized markers resolved at compile time
11//!
12//! # Usage
13//!
14//! Mechanics declare their execution characteristics via the `Execution` associated type:
15//!
16//! ```ignore
17//! impl Mechanic for CombatMechanic {
18//! type Execution = ParallelSafe;
19//! // ...
20//! }
21//! ```
22//!
23//! ECS adapters (issun-bevy) read these hints to schedule systems appropriately:
24//!
25//! ```ignore
26//! if M::Execution::PARALLEL_SAFE {
27//! app.add_systems(Update, system::<M>);
28//! } else {
29//! app.add_systems(Update, system::<M>.in_set(SequentialSet));
30//! }
31//! ```
32
33/// Trait for execution characteristic hints.
34///
35/// This trait is implemented by zero-sized marker types that communicate
36/// how a mechanic prefers to be executed in an ECS environment.
37///
38/// # Associated Constants
39///
40/// - `PARALLEL_SAFE`: Whether this mechanic can safely run in parallel with others
41/// - `PREFERRED_SCHEDULE`: Optional hint for which schedule/set to use
42/// - `ORDERING_HINT`: Optional hint for relative ordering (e.g., "after:health_regen")
43///
44/// # Examples
45///
46/// ```
47/// use issun_core::mechanics::execution::{ExecutionHint, ParallelSafe};
48///
49/// // Check if a mechanic is parallel-safe
50/// assert!(ParallelSafe::PARALLEL_SAFE);
51/// assert_eq!(ParallelSafe::PREFERRED_SCHEDULE, None);
52/// ```
53pub trait ExecutionHint {
54 /// Whether this mechanic can safely run in parallel with other mechanics.
55 ///
56 /// - `true`: No data races or ordering dependencies (default)
57 /// - `false`: Requires sequential execution or specific ordering
58 const PARALLEL_SAFE: bool = true;
59
60 /// Preferred schedule or system set name.
61 ///
62 /// This is an optional hint for which Bevy schedule or system set
63 /// this mechanic should be added to.
64 ///
65 /// Examples: `"Update"`, `"FixedUpdate"`, `"PostUpdate"`
66 const PREFERRED_SCHEDULE: Option<&'static str> = None;
67
68 /// Ordering hint relative to other systems.
69 ///
70 /// Format: `"after:label"` or `"before:label"`
71 ///
72 /// Examples:
73 /// - `"after:health_regen"` - Run after health regeneration
74 /// - `"before:death_check"` - Run before death checking
75 const ORDERING_HINT: Option<&'static str> = None;
76}
77
78// ============================================================================
79// Standard ExecutionHint Implementations
80// ============================================================================
81
82/// Parallel-safe execution (default).
83///
84/// Use this when your mechanic:
85/// - Only reads/writes to the entity it's processing
86/// - Has no dependencies on other mechanics' execution order
87/// - Can safely run concurrently with other mechanics
88///
89/// # Examples
90///
91/// ```ignore
92/// impl Mechanic for SimpleDamageMechanic {
93/// type Execution = ParallelSafe; // Can run in parallel
94/// // ...
95/// }
96/// ```
97///
98/// Most mechanics should use this as it allows maximum parallelism.
99pub struct ParallelSafe;
100
101impl ExecutionHint for ParallelSafe {
102 const PARALLEL_SAFE: bool = true;
103}
104
105/// Sequential execution with ordering dependency.
106///
107/// Use this as a base for creating specific ordering constraints.
108/// For common patterns, use the provided marker types or define your own.
109///
110/// # Examples
111///
112/// ```ignore
113/// // Define a custom ordering
114/// pub struct AfterHealthRegen;
115/// impl ExecutionHint for AfterHealthRegen {
116/// const PARALLEL_SAFE: bool = false;
117/// const ORDERING_HINT: Option<&'static str> = Some("health_regen");
118/// }
119///
120/// impl Mechanic for CombatMechanic {
121/// type Execution = AfterHealthRegen;
122/// // ...
123/// }
124/// ```
125pub struct SequentialAfter;
126
127impl ExecutionHint for SequentialAfter {
128 const PARALLEL_SAFE: bool = false;
129}
130
131/// Sequential execution before other systems.
132///
133/// Similar to `SequentialAfter`, but indicates this mechanic should
134/// run before certain other systems.
135pub struct SequentialBefore;
136
137impl ExecutionHint for SequentialBefore {
138 const PARALLEL_SAFE: bool = false;
139}
140
141/// Transactional execution.
142///
143/// Use this when your mechanic:
144/// - Reads from or writes to multiple entities
145/// - Requires consistent view of the world (no partial updates)
146/// - Should not interleave with other mechanics
147///
148/// # Examples
149///
150/// ```ignore
151/// // Propagation reads from neighbors, needs consistency
152/// impl Mechanic for PropagationMechanic {
153/// type Execution = Transactional;
154/// // ...
155/// }
156/// ```
157///
158/// This is a stronger constraint than `SequentialAfter` - it suggests
159/// the mechanic should run in complete isolation.
160pub struct Transactional;
161
162impl ExecutionHint for Transactional {
163 const PARALLEL_SAFE: bool = false;
164 const PREFERRED_SCHEDULE: Option<&'static str> = Some("Sequential");
165}
166
167/// Fixed timestep execution.
168///
169/// Use this when your mechanic requires a fixed timestep (e.g., physics simulation).
170///
171/// # Examples
172///
173/// ```ignore
174/// impl Mechanic for PhysicsMechanic {
175/// type Execution = FixedTimestep;
176/// // ...
177/// }
178/// ```
179pub struct FixedTimestep;
180
181impl ExecutionHint for FixedTimestep {
182 const PARALLEL_SAFE: bool = true;
183 const PREFERRED_SCHEDULE: Option<&'static str> = Some("FixedUpdate");
184}
185
186/// Post-update execution.
187///
188/// Use this when your mechanic should run after all regular updates
189/// (e.g., cleanup, logging, analytics).
190///
191/// # Examples
192///
193/// ```ignore
194/// impl Mechanic for LoggingMechanic {
195/// type Execution = PostUpdate;
196/// // ...
197/// }
198/// ```
199pub struct PostUpdate;
200
201impl ExecutionHint for PostUpdate {
202 const PARALLEL_SAFE: bool = true;
203 const PREFERRED_SCHEDULE: Option<&'static str> = Some("PostUpdate");
204}
205
206#[cfg(test)]
207#[allow(clippy::assertions_on_constants)]
208mod tests {
209 use super::*;
210
211 #[test]
212 fn test_parallel_safe_constants() {
213 assert!(ParallelSafe::PARALLEL_SAFE);
214 assert_eq!(ParallelSafe::PREFERRED_SCHEDULE, None);
215 assert_eq!(ParallelSafe::ORDERING_HINT, None);
216 }
217
218 #[test]
219 fn test_sequential_after() {
220 assert!(!SequentialAfter::PARALLEL_SAFE);
221 assert_eq!(SequentialAfter::ORDERING_HINT, None);
222 }
223
224 #[test]
225 fn test_sequential_before() {
226 assert!(!SequentialBefore::PARALLEL_SAFE);
227 }
228
229 #[test]
230 fn test_transactional() {
231 assert!(!Transactional::PARALLEL_SAFE);
232 assert_eq!(Transactional::PREFERRED_SCHEDULE, Some("Sequential"));
233 }
234
235 #[test]
236 fn test_fixed_timestep() {
237 assert!(FixedTimestep::PARALLEL_SAFE);
238 assert_eq!(FixedTimestep::PREFERRED_SCHEDULE, Some("FixedUpdate"));
239 }
240
241 #[test]
242 fn test_post_update() {
243 assert!(PostUpdate::PARALLEL_SAFE);
244 assert_eq!(PostUpdate::PREFERRED_SCHEDULE, Some("PostUpdate"));
245 }
246
247 #[test]
248 fn test_zero_sized() {
249 // All execution hints should be zero-sized
250 assert_eq!(std::mem::size_of::<ParallelSafe>(), 0);
251 assert_eq!(std::mem::size_of::<SequentialAfter>(), 0);
252 assert_eq!(std::mem::size_of::<SequentialBefore>(), 0);
253 assert_eq!(std::mem::size_of::<Transactional>(), 0);
254 assert_eq!(std::mem::size_of::<FixedTimestep>(), 0);
255 assert_eq!(std::mem::size_of::<PostUpdate>(), 0);
256 }
257}