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
#![deny(missing_docs)]
extern crate hibitset;
extern crate shrev;
extern crate specs;
use hibitset::BitSetLike;
use shrev::{EventChannel, EventIterator};
use specs::prelude::*;
use specs::storage::{MaskedStorage, TryDefault, UnprotectedStorage};
use specs::world::Index;
use std::any::Any;
use std::ops::{Deref, DerefMut};
pub struct MirroredStorage<C: Mirrored, S = DenseVecStorage<C>> {
chan: EventChannel<C::Event>,
store: S,
}
pub trait Mirrored {
type Event: shrev::Event;
fn insert(&mut self, _chan: &mut EventChannel<Self::Event>, _id: Index) {}
fn remove(&mut self, _chan: &mut EventChannel<Self::Event>, _id: Index) {}
}
impl<C: Mirrored, S: UnprotectedStorage<C>> MirroredStorage<C, S> {
unsafe fn modify(&mut self, id: Index) -> (&mut C, &mut EventChannel<C::Event>) {
(self.store.get_mut(id), &mut self.chan)
}
}
impl<C: Mirrored, S> Default for MirroredStorage<C, S>
where
S: TryDefault,
{
fn default() -> Self {
MirroredStorage {
chan: EventChannel::new(),
store: S::unwrap_default(),
}
}
}
impl<C, S> UnprotectedStorage<C> for MirroredStorage<C, S>
where
C: Mirrored + Component,
S: UnprotectedStorage<C>,
{
unsafe fn clean<B>(&mut self, has: B)
where
B: BitSetLike,
{
self.store.clean(has)
}
unsafe fn get(&self, id: Index) -> &C {
self.store.get(id)
}
unsafe fn get_mut(&mut self, id: Index) -> &mut C {
self.store.get_mut(id)
}
unsafe fn insert(&mut self, id: Index, mut comp: C) {
comp.insert(&mut self.chan, id);
self.store.insert(id, comp);
}
unsafe fn remove(&mut self, id: Index) -> C {
let mut comp = self.store.remove(id);
comp.remove(&mut self.chan, id);
comp
}
}
pub trait StorageExt<C: Mirrored> {
fn read_events(&self, reader: &mut ReaderId<C::Event>) -> EventIterator<C::Event>;
}
pub trait StorageMutExt<C: Mirrored>: StorageExt<C> {
fn register_reader(&mut self) -> ReaderId<C::Event> {
self.event_channel().register_reader()
}
fn modify(&mut self, entity: Entity) -> Option<(&mut C, &mut EventChannel<C::Event>)>;
fn event_channel(&mut self) -> &mut EventChannel<C::Event>;
}
impl<'a, C, S, D> StorageExt<C> for Storage<'a, C, D>
where
C: Mirrored + Component<Storage = MirroredStorage<C, S>>,
S: UnprotectedStorage<C> + Any + Send + Sync,
D: Deref<Target = MaskedStorage<C>>,
{
fn read_events(&self, reader: &mut ReaderId<C::Event>) -> EventIterator<C::Event> {
self.unprotected_storage().chan.read(reader)
}
}
impl<'a, C, S, D> StorageMutExt<C> for Storage<'a, C, D>
where
C: Mirrored + Component<Storage = MirroredStorage<C, S>>,
S: UnprotectedStorage<C> + Any + Send + Sync,
D: DerefMut<Target = MaskedStorage<C>>,
{
fn modify(&mut self, entity: Entity) -> Option<(&mut C, &mut EventChannel<C::Event>)> {
if self.contains(entity) {
Some(unsafe { self.unprotected_storage_mut().modify(entity.id()) })
} else {
None
}
}
fn event_channel(&mut self) -> &mut EventChannel<C::Event> {
unsafe { &mut self.unprotected_storage_mut().chan }
}
}