summaryrefslogtreecommitdiff
path: root/ecs/src/component/local.rs
blob: ad79f6fa0aa24b72562683645b0bec0922762b34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::ops::{Deref, DerefMut};

use crate::component::Component;
use crate::system::{ComponentRefMut, Param as SystemParam, System};
use crate::World;

/// Holds a component which is local to a single system.
#[derive(Debug)]
pub struct Local<'world, LocalComponent: Component>
{
    local_component: ComponentRefMut<'world, LocalComponent>,
}

impl<'world, LocalComponent> SystemParam<'world> for Local<'world, LocalComponent>
where
    LocalComponent: Component,
{
    type Input = LocalComponent;

    fn initialize<SystemImpl>(
        system: &mut impl System<'world, SystemImpl>,
        input: Self::Input,
    )
    {
        system.set_local_component(input);
    }

    fn new<SystemImpl>(
        system: &'world impl System<'world, SystemImpl>,
        _world: &'world World,
    ) -> Self
    {
        let local_component = system
            .get_local_component_mut::<LocalComponent>()
            .expect("Local component is uninitialized");

        Self { local_component }
    }
}

impl<'world, LocalComponent> Deref for Local<'world, LocalComponent>
where
    LocalComponent: Component,
{
    type Target = LocalComponent;

    fn deref(&self) -> &Self::Target
    {
        &self.local_component
    }
}

impl<'world, LocalComponent> DerefMut for Local<'world, LocalComponent>
where
    LocalComponent: Component,
{
    fn deref_mut(&mut self) -> &mut Self::Target
    {
        &mut self.local_component
    }
}