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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use core::{any::TypeId, marker::PhantomData, ptr::NonNull};

use crate::{Access, Archetype, Component, Fetch, Query};

/// Query that retrieves mutation state of type `T` component.
/// Added components do not count as mutated.
///
/// It is your responsibility to clear trackers with [`World::clear_trackers()`](crate::World::clear_trackers())
/// at the start of the frame (or any other suitable moment).
///
/// # Example
/// ```
/// # use hecs::*;
/// let mut world = World::new();
/// let e = world.spawn((123,));
/// for (_id, (value, value_mut)) in world.query::<(&i32, Mutated<i32>)>().iter() {
///   assert_eq!(*value, 123, "!1");
///   assert_eq!(value_mut, false, "!2");
/// }
/// for (_id, mut value) in world.query::<&mut i32>().iter() {
///   *value = 42;
/// }
/// for (_id, (value, value_mut)) in world.query::<(&i32, Mutated<i32>)>().iter() {
///   assert_eq!(*value, 42, "!3");
///   assert_eq!(value_mut, true, "!3a");
/// }
/// world.clear_trackers();
/// for (_id, value_mut) in world.query::<Mutated<i32>>().iter() {
///   assert_eq!(value_mut, false, "!4");
/// }
/// ```
pub struct Mutated<T>(PhantomData<fn(T)>);

impl<T: Component> Query for Mutated<T> {
	type Item<'a> = bool;
	
    type Fetch = FetchMutated<T>;
    
    unsafe fn get<'a>(fetch: &FetchMutated<T>, n: usize) -> Self::Item<'a> {
        *fetch.0.as_ptr().add(n)
    }
}

#[doc(hidden)]
pub struct FetchMutated<T>(NonNull<bool>, PhantomData<fn(T)>);

unsafe impl<T: Component> Fetch for FetchMutated<T> {
    type State = usize;

    fn dangling() -> Self {
        Self(NonNull::dangling(), PhantomData)
    }

    fn access(archetype: &Archetype) -> Option<Access> {
        if archetype.has::<T>() {
            Some(Access::Read)
        } else {
            None
        }
    }

    fn borrow(_archetype: &Archetype, _state: Self::State) {}
    fn prepare(archetype: &Archetype) -> Option<Self::State> {
        archetype.get_state::<T>()
    }
    fn execute(archetype: &Archetype, state: Self::State) -> Self {
        Self(archetype.get_mutated(state), PhantomData)
    }
    fn release(_archetype: &Archetype, _state: Self::State) {}

    fn for_each_borrow(mut f: impl FnMut(TypeId, bool)) {
        f(TypeId::of::<T>(), false);
    }
}

/// Query that retrieves added state of type `T` component.
///
/// It is your responsibility to clear trackers with [`World::clear_trackers()`](crate::World::clear_trackers())
/// at the start of the frame (or any other suitable moment).
///
/// # Example
/// ```
/// # use hecs::*;
/// let mut world = World::new();
/// let e = world.spawn((123,));
/// for (_id, (value, value_add)) in world.query::<(&i32, Added<i32>)>().iter() {
///   assert_eq!(*value, 123);
///   assert_eq!(value_add, true);
/// }
/// world.clear_trackers();
/// for (_id, value_add) in world.query::<Added<i32>>().iter() {
///   assert_eq!(value_add, false);
/// }
/// ```
pub struct Added<T>(PhantomData<fn(T)>);

impl<T: Component> Query for Added<T> {
	type Item<'a> = bool;
	
    type Fetch = FetchAdded<T>;
    
    unsafe fn get<'a>(fetch: &FetchAdded<T>, n: usize) -> Self::Item<'a> {
        *fetch.0.as_ptr().add(n)
    }
}

#[doc(hidden)]
pub struct FetchAdded<T>(NonNull<bool>, PhantomData<fn(T)>);

unsafe impl<T: Component> Fetch for FetchAdded<T> {
    type State = usize;

    fn dangling() -> Self {
        Self(NonNull::dangling(), PhantomData)
    }

    fn access(archetype: &Archetype) -> Option<Access> {
        if archetype.has::<T>() {
            Some(Access::Read)
        } else {
            None
        }
    }

    fn borrow(_archetype: &Archetype, _state: Self::State) {}
    fn prepare(archetype: &Archetype) -> Option<Self::State> {
        archetype.get_state::<T>()
    }
    fn execute(archetype: &Archetype, state: Self::State) -> Self {
        Self(archetype.get_added(state), PhantomData)
    }
    fn release(_archetype: &Archetype, _state: Self::State) {}

    fn for_each_borrow(mut f: impl FnMut(TypeId, bool)) {
        f(TypeId::of::<T>(), false);
    }
}

/// Query that retrieves changed state of type `T` component.
/// Changed component is one that have either been mutated or added.
///
/// It is your responsibility to clear trackers with [`World::clear_trackers()`](crate::World::clear_trackers())
/// at the start of the frame (or any other suitable moment).
///
/// # Example
/// ```
/// # use hecs::*;
/// let mut world = World::new();
/// let e = world.spawn((123,));
/// for (_id, (value, value_ch)) in world.query::<(&i32, Changed<i32>)>().iter() {
///   assert_eq!(*value, 123);
///   assert_eq!(value_ch, true);
/// }
/// world.clear_trackers();
/// for (_id, value_ch) in world.query::<Changed<i32>>().iter() {
///   assert_eq!(value_ch, false);
/// }
/// for (_id, mut value) in world.query::<&mut i32>().iter() {
///   *value = 42;
/// }
/// for (_id, (value, value_ch)) in world.query::<(&i32, Changed<i32>)>().iter() {
///   assert_eq!(*value, 42);
///   assert_eq!(value_ch, true);
/// }
/// world.clear_trackers();
/// for (_id, value_ch) in world.query::<Changed<i32>>().iter() {
///   assert_eq!(value_ch, false);
/// }
/// ```
pub struct Changed<T>(PhantomData<fn(T)>);

impl<T: Component> Query for Changed<T> {
	type Item<'a> = bool;
	
    type Fetch = FetchChanged<T>;
    
    unsafe fn get<'a>(fetch: &FetchChanged<T>, n: usize) -> Self::Item<'a> {
        *fetch.0.as_ptr().add(n) || *fetch.1.as_ptr().add(n)
    }
}

#[doc(hidden)]
pub struct FetchChanged<T>(NonNull<bool>, NonNull<bool>, PhantomData<fn(T)>);

unsafe impl<T: Component> Fetch for FetchChanged<T> {
    type State = usize;

    fn dangling() -> Self {
        Self(NonNull::dangling(), NonNull::dangling(), PhantomData)
    }

    fn access(archetype: &Archetype) -> Option<Access> {
        if archetype.has::<T>() {
            Some(Access::Read)
        } else {
            None
        }
    }

    fn borrow(_archetype: &Archetype, _state: Self::State) {}
    fn prepare(archetype: &Archetype) -> Option<Self::State> {
        archetype.get_state::<T>()
    }
    fn execute(archetype: &Archetype, state: Self::State) -> Self {
        Self(
            archetype.get_mutated(state),
            archetype.get_added(state),
            PhantomData,
        )
    }
    fn release(_archetype: &Archetype, _state: Self::State) {}

    fn for_each_borrow(mut f: impl FnMut(TypeId, bool)) {
        f(TypeId::of::<T>(), false);
    }
}