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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use core::{any::TypeId, ptr::NonNull};

use crate::{
    archetype::Archetype, component::ComponentInfo, system::ActionBufferQueue, world::World, Access,
};

use super::{FnArg, FnArgState};

#[derive(Default)]
pub struct WorldReadState;

impl FnArg for &World {
    type State = WorldReadState;
}

unsafe impl FnArgState for WorldReadState {
    type Arg<'a> = &'a World;

    #[inline(always)]
    fn new() -> Self {
        Self::default()
    }

    #[inline(always)]
    fn is_local(&self) -> bool {
        false
    }

    #[inline(always)]
    fn world_access(&self) -> Option<Access> {
        Some(Access::Read)
    }

    #[inline(always)]
    fn visit_archetype(&self, _archetype: &Archetype) -> bool {
        true
    }

    #[inline(always)]
    fn borrows_components_at_runtime(&self) -> bool {
        true
    }

    #[inline(always)]
    fn component_access(&self, _comp: &ComponentInfo) -> Option<Access> {
        Some(Access::Write)
    }

    #[inline(always)]
    fn resource_type_access(&self, _ty: TypeId) -> Option<Access> {
        Some(Access::Write)
    }

    #[inline(always)]
    unsafe fn get_unchecked<'a>(
        &'a mut self,
        world: NonNull<World>,
        _queue: &mut dyn ActionBufferQueue,
    ) -> &'a World {
        // Safety: Declares read.
        unsafe { world.as_ref() }
    }
}

#[derive(Default)]
pub struct WorldWriteState;

impl FnArg for &mut World {
    type State = WorldWriteState;
}

unsafe impl FnArgState for WorldWriteState {
    type Arg<'a> = &'a mut World;

    #[inline(always)]
    fn new() -> Self {
        Self::default()
    }

    #[inline(always)]
    fn is_local(&self) -> bool {
        true
    }

    #[inline(always)]
    fn world_access(&self) -> Option<Access> {
        Some(Access::Write)
    }

    #[inline(always)]
    fn visit_archetype(&self, _archetype: &Archetype) -> bool {
        true
    }

    #[inline(always)]
    fn borrows_components_at_runtime(&self) -> bool {
        false
    }

    #[inline(always)]
    fn component_access(&self, _comp: &ComponentInfo) -> Option<Access> {
        Some(Access::Write)
    }

    #[inline(always)]
    fn resource_type_access(&self, _ty: TypeId) -> Option<Access> {
        Some(Access::Write)
    }

    #[inline(always)]
    unsafe fn get_unchecked<'a>(
        &'a mut self,
        mut world: NonNull<World>,
        _queue: &mut dyn ActionBufferQueue,
    ) -> &'a mut World {
        // Safety: Declares write.
        unsafe { world.as_mut() }
    }
}