Skip to main content

goud_engine/ecs/system/system_param/
static_param.rs

1//! [`StaticSystemParam`] and [`StaticSystemParamState`] — state-backed system parameters.
2
3use crate::ecs::query::Access;
4use crate::ecs::World;
5
6use super::traits::{SystemParam, SystemParamState};
7
8// =============================================================================
9// StaticSystemParam
10// =============================================================================
11
12/// A system parameter that provides access to its inner state directly.
13///
14/// `StaticSystemParam` is useful when you want to store data in the parameter
15/// state and access it during system execution. Unlike `Local<T>`, this
16/// stores the data in the system's state rather than per-system instance.
17///
18/// # Example
19///
20/// ```
21/// use goud_engine::ecs::World;
22/// use goud_engine::ecs::query::Access;
23/// use goud_engine::ecs::system::{SystemParam, SystemParamState, StaticSystemParam};
24///
25/// // State that tracks how many times the system ran
26/// #[derive(Default)]
27/// struct RunCountState {
28///     count: u32,
29/// }
30///
31/// impl SystemParamState for RunCountState {
32///     fn init(_world: &mut World) -> Self {
33///         Self::default()
34///     }
35/// }
36///
37/// // The parameter type
38/// type RunCount = StaticSystemParam<RunCountState>;
39/// ```
40#[derive(Debug)]
41pub struct StaticSystemParam<S: SystemParamState> {
42    _marker: std::marker::PhantomData<fn() -> S>,
43}
44
45/// State wrapper for [`StaticSystemParam`].
46#[derive(Debug)]
47pub struct StaticSystemParamState<S> {
48    inner: S,
49}
50
51/// State for StaticSystemParam - just wraps the inner state
52impl<S: SystemParamState> SystemParamState for StaticSystemParamState<S> {
53    #[inline]
54    fn init(world: &mut World) -> Self {
55        Self {
56            inner: S::init(world),
57        }
58    }
59
60    #[inline]
61    fn apply(&mut self, world: &mut World) {
62        self.inner.apply(world);
63    }
64}
65
66impl<S> StaticSystemParamState<S> {
67    /// Returns a reference to the inner state.
68    #[inline]
69    pub fn get(&self) -> &S {
70        &self.inner
71    }
72
73    /// Returns a mutable reference to the inner state.
74    #[inline]
75    pub fn get_mut(&mut self) -> &mut S {
76        &mut self.inner
77    }
78}
79
80impl<S: SystemParamState + 'static> SystemParam for StaticSystemParam<S> {
81    type State = StaticSystemParamState<S>;
82    type Item<'w, 's> = &'s mut S;
83
84    #[inline]
85    fn update_access(_state: &Self::State, _access: &mut Access) {
86        // Static state doesn't access world components
87    }
88
89    #[inline]
90    fn get_param<'w, 's>(state: &'s mut Self::State, _world: &'w World) -> Self::Item<'w, 's> {
91        state.get_mut()
92    }
93}