-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathtrack.rs
157 lines (133 loc) · 4.94 KB
/
track.rs
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
use std::iter::Extend;
use std::ops::{Deref, DerefMut};
use shrev::{EventChannel, ReaderId};
use join::Join;
use storage::{MaskedStorage, Storage};
use world::{Component, Index};
/// `UnprotectedStorage`s that track modifications, insertions, and
/// removals of components.
pub trait Tracked {
/// Event channel tracking modified components.
fn modified(&self) -> &EventChannel<ModifiedFlag>;
/// Mutable event channel tracking modified components.
fn modified_mut(&mut self) -> &mut EventChannel<ModifiedFlag>;
/// Event channel tracking inserted components.
fn inserted(&self) -> &EventChannel<InsertedFlag>;
/// Mutable event channel tracking inserted components.
fn inserted_mut(&mut self) -> &mut EventChannel<InsertedFlag>;
/// Event channel tracking removed components.
fn removed(&self) -> &EventChannel<RemovedFlag>;
/// Mutable event channel tracking removed components.
fn removed_mut(&mut self) -> &mut EventChannel<RemovedFlag>;
}
impl<'e, T, D> Storage<'e, T, D>
where
T: Component,
T::Storage: Tracked,
D: Deref<Target = MaskedStorage<T>>,
{
/// Returns the event channel tracking modified components.
pub fn modified(&self) -> &EventChannel<ModifiedFlag> {
self.open().1.modified()
}
/// Returns the event channel tracking inserted components.
pub fn inserted(&self) -> &EventChannel<InsertedFlag> {
self.open().1.inserted()
}
/// Returns the event channel tracking removed components.
pub fn removed(&self) -> &EventChannel<RemovedFlag> {
self.open().1.removed()
}
/// Reads events from the modified `EventChannel` and populates a structure using the events.
pub fn populate_modified<E>(&self, reader_id: &mut ReaderId<ModifiedFlag>, value: &mut E)
where
E: Extend<Index>,
{
value.extend(self.modified().read(reader_id).map(|flag| *flag.as_ref()));
}
/// Reads events from the inserted `EventChannel` and populates a structure using the events.
pub fn populate_inserted<E>(&self, reader_id: &mut ReaderId<InsertedFlag>, value: &mut E)
where
E: Extend<Index>,
{
value.extend(self.inserted().read(reader_id).map(|flag| *flag.as_ref()));
}
/// Reads events from the removed `EventChannel` and populates a structure using the events.
pub fn populate_removed<E>(&self, reader_id: &mut ReaderId<RemovedFlag>, value: &mut E)
where
E: Extend<Index>,
{
value.extend(self.removed().read(reader_id).map(|flag| *flag.as_ref()));
}
}
impl<'e, T, D> Storage<'e, T, D>
where
T: Component,
T::Storage: Tracked,
D: DerefMut<Target = MaskedStorage<T>>,
{
/// Returns the event channel tracking modified components mutably.
pub fn modified_mut(&mut self) -> &mut EventChannel<ModifiedFlag> {
self.open().1.modified_mut()
}
/// Returns the event channel tracking inserted components mutably.
pub fn inserted_mut(&mut self) -> &mut EventChannel<InsertedFlag> {
self.open().1.inserted_mut()
}
/// Returns the event channel tracking removed components mutably.
pub fn removed_mut(&mut self) -> &mut EventChannel<RemovedFlag> {
self.open().1.removed_mut()
}
/// Starts tracking modified events.
pub fn track_modified(&mut self) -> ReaderId<ModifiedFlag> {
self.open().1.modified_mut().register_reader()
}
/// Starts tracking inserted events.
pub fn track_inserted(&mut self) -> ReaderId<InsertedFlag> {
self.open().1.inserted_mut().register_reader()
}
/// Starts tracking removed events.
pub fn track_removed(&mut self) -> ReaderId<RemovedFlag> {
self.open().1.removed_mut().register_reader()
}
/// Flags an index as modified.
pub fn flag_modified(&mut self, id: Index) {
self.modified_mut().single_write(id.into());
}
/// Flags an index as inserted.
pub fn flag_inserted(&mut self, id: Index) {
self.inserted_mut().single_write(id.into());
}
/// Flags an index as removed.
pub fn flag_removed(&mut self, id: Index) {
self.removed_mut().single_write(id.into());
}
}
macro_rules! flag {
( $( $name:ident ),* ) => {
$(
/// Flag with additional type safety against which kind of
/// operations were done.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct $name(Index);
impl Deref for $name {
type Target = Index;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl AsRef<Index> for $name {
fn as_ref(&self) -> &Index {
&self.0
}
}
impl From<Index> for $name {
fn from(flag: Index) -> Self {
$name(flag)
}
}
)*
}
}
// Separate types for type safety reasons.
flag!(ModifiedFlag, InsertedFlag, RemovedFlag);