Skip to main content

pebble/ecs/
local.rs

1use crate::ecs::{resources::Resources, system_param::SystemParam};
2
3/// Per-system persistent state — each system that takes `Local<T>` gets its
4/// own private `T`, starting at `T::default()`, that survives between
5/// ticks. Two different systems, even with the same `T`, never share one.
6pub struct Local<'a, T: Default + hecs::Component> {
7    data: &'a mut T,
8}
9
10impl<'a, T: Default + Send + Sync + 'static> std::ops::Deref for Local<'a, T> {
11    type Target = T;
12    fn deref(&self) -> &Self::Target {
13        self.data
14    }
15}
16
17impl<'a, T: Default + Send + Sync + 'static> std::ops::DerefMut for Local<'a, T> {
18    fn deref_mut(&mut self) -> &mut Self::Target {
19        self.data
20    }
21}
22
23impl<T: Default + Send + Sync + 'static> SystemParam for Local<'_, T> {
24    type Item<'w> = Local<'w, T>;
25    type State = T;
26
27    fn fetch<'w>(
28        _world: &'w hecs::World,
29        _resources: &'w Resources,
30        state: &'w mut Self::State,
31    ) -> Self::Item<'w> {
32        Local { data: state }
33    }
34}