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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use primitives::{Component, Id, Valid};
use service::ServiceBase;

use downcast::Downcast;

use std::borrow::Borrow;

// ++++++++++++++++++++ StoreBase ++++++++++++++++++++

pub trait StoreBase: ServiceBase {
    type Id: Id;

    fn len(&self) -> usize;

    fn is_assigned<'id>(&self, id: Valid<'id, Self::Id>) -> bool;

    /*fn assign_cloned<'id>(
        &mut self, 
        dest: Valid<'id, Self::Id>, 
        src: Valid<'id, Self::Id>,
    ) -> bool;*/

    /// A variant of `.release(id)` which drops the returned component. 
    /// Returns `true` if sucessful.
    fn release_drop<'id>(&mut self, id: Valid<'id, Self::Id>) -> bool;

    fn clear(&mut self);
}

impl<ID: Id, T: StoreBase<Id = ID>> Downcast<T> for StoreBase<Id = ID> {
    impl_downcast_items!(T);
}

impl<ID: Id> StoreBase<Id = ID> {
    downcast_method_items!();
}

// ++++++++++++++++++++ ComponentStore ++++++++++++++++++++

/// Workaround for lack of HK-lifetimes.
pub trait _ComponentStore<'a>: StoreBase {
    type _Value: Component;

    type Values: Iterator<Item = &'a Self::_Value> + 'a;
    
    type ValuesMut: Iterator<Item = &'a mut Self::_Value> + 'a;
}

pub trait ComponentStore: StoreBase
    where Self: for<'a> _ComponentStore<'a, _Value = <Self as ComponentStore>::Value>
{
    type Value: Component; 

    fn assign<'id, T>(&mut self, id: Valid<'id, Self::Id>, com: T) -> Option<Self::Value>
        where T: Into<Self::Value>;

    fn release<'id>(&mut self, id: Valid<'id, Self::Id>) -> Option<Self::Value>;

    /// Returns `None` if `id` is not assigned.
    fn get<'id>(&self, id: Valid<'id, Self::Id>) -> Option<&Self::Value>;

    /// Returns `None` if `id` is not assigned.
    fn get_mut<'id>(&mut self, id: Valid<'id, Self::Id>) -> Option<&mut Self::Value>;

    fn values<'a>(&'a self) -> <Self as _ComponentStore<'a>>::Values;

    fn values_mut<'a>(&'a mut self) -> <Self as _ComponentStore<'a>>::ValuesMut;
}

// ++++++++++++++++++++ ComponentStoreIter ++++++++++++++++++++

/// Workaround for lack of HK-lifetimes.
pub trait _ComponentStoreIter<'a>: _ComponentStore<'a> {
    type Iter: Iterator<Item = (Valid<'a, Self::Id>, &'a Self::_Value)> + 'a;

    type IterMut: Iterator<Item = (Valid<'a, Self::Id>, &'a mut Self::_Value)> + 'a;
}

pub trait ComponentStoreIter: ComponentStore
    where Self: for<'a> _ComponentStoreIter<'a>
{
    fn iter<'a>(&'a self) -> <Self as _ComponentStoreIter<'a>>::Iter;

    fn iter_mut<'a>(&'a mut self) -> <Self as _ComponentStoreIter<'a>>::IterMut;
}

// ++++++++++++++++++++ EntityStore ++++++++++++++++++++

pub trait EntityStore: StoreBase {
    fn assign<'id>(&mut self, id: Valid<'id, Self::Id>) -> bool;

    fn release<'id>(&mut self, id: Valid<'id, Self::Id>) -> bool {
        self.release_drop(id)
    }
}

// ++++++++++++++++++++ EntityStoreIter ++++++++++++++++++++

pub trait _EntityStoreIter<'a>: EntityStore {
    type Iter: Iterator<Item = Valid<'a, Self::Id>>;
}

pub trait EntityStoreIter: EntityStore
    where Self: for<'a> _EntityStoreIter<'a>
{
    fn iter<'a>(&'a self) -> <Self as _EntityStoreIter<'a>>::Iter;
}

// ++++++++++++++++++++ StoreWithDirtyState ++++++++++++++++++++

/// * `assign -> Dirtied::Assigned`
/// * `{release, clear} -> Dirtied::Released`
/// * `{get_mut, iter_mut} -> Dirtied::Modified`
/// * `release & assign -> Dirtied::Modified`
/// * `assign & release` -> (nothing)`
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Dirtied<'id, ID: Id> {
    Assigned(Valid<'id, ID>),
    Released(ID),
    Modified(Valid<'id, ID>),
}

impl<'id, ID: Id> Borrow<ID> for Dirtied<'id, ID> {
    fn borrow(&self) -> &ID {
        match self {
            &Dirtied::Assigned(ref id) => &*id,
            &Dirtied::Released(ref id) => &id,
            &Dirtied::Modified(ref id) => &*id,
        }
    }
}

/// Workaround for lack of HK-lifetimes.
pub trait _StoreWithDirtyState<'a>: StoreBase {
    type DirtyEntities: Iterator<Item = Dirtied<'a, Self::Id>>;
}

pub trait StoreWithDirtyState: StoreBase 
    where Self: for<'a> _StoreWithDirtyState<'a>
{
    fn was_cleared(&self) -> bool;

    fn dirty_entities<'a>(&'a self) -> <Self as _StoreWithDirtyState<'a>>::DirtyEntities;
}

// ++++++++++++++++++++ newtype-impls ++++++++++++++++++++

use std::ops::DerefMut;

impl<S> StoreBase for S
    where S: DerefMut + ServiceBase, S::Target: StoreBase
{
    type Id = <S::Target as StoreBase>::Id;

    fn len(&self) -> usize { 
        (**self).len() 
    }

    fn is_assigned<'id>(&self, id: Valid<'id, Self::Id>) -> bool { 
        (**self).is_assigned(id) 
    }

    /*fn assign_cloned<'id>(
        &mut self, 
        dest: Valid<'id, Self::Id>, 
        src: Valid<'id, Self::Id>,
    ) -> bool {
        (**self).assign_cloned(dest, src)
    }*/

    fn release_drop<'id>(&mut self, id: Valid<'id, Self::Id>) -> bool {
        (**self).release_drop(id)
    }

    fn clear(&mut self){
        (**self).clear()
    }
}

impl<'a, S> _ComponentStore<'a> for S
    where S: DerefMut + ServiceBase, S::Target: _ComponentStore<'a>
{
    type _Value = <S::Target as _ComponentStore<'a>>::_Value;

    type Values = <S::Target as _ComponentStore<'a>>::Values;
    
    type ValuesMut = <S::Target as _ComponentStore<'a>>::ValuesMut;
}

impl<S> ComponentStore for S
    where S: DerefMut + ServiceBase, S::Target: ComponentStore
{
    type Value = <S::Target as ComponentStore>::Value; 

    fn assign<'id, T>(&mut self, id: Valid<'id, Self::Id>, com: T) -> Option<Self::Value>
        where T: Into<Self::Value>
    {
        (**self).assign(id, com)
    }

    fn release<'id>(&mut self, id: Valid<'id, Self::Id>) -> Option<Self::Value> {
        (**self).release(id)
    }

    fn get<'id>(&self, id: Valid<'id, Self::Id>) -> Option<&Self::Value> {
        (**self).get(id)
    }

    fn get_mut<'id>(&mut self, id: Valid<'id, Self::Id>) -> Option<&mut Self::Value> {
        (**self).get_mut(id)
    }

    fn values<'a>(&'a self) -> <Self as _ComponentStore<'a>>::Values {
        (**self).values()
    }

    fn values_mut<'a>(&'a mut self) -> <Self as _ComponentStore<'a>>::ValuesMut {
        (**self).values_mut()
    }
}

impl<'a, S> _ComponentStoreIter<'a> for S
    where S: DerefMut + ServiceBase, S::Target: _ComponentStoreIter<'a>
{
    type Iter = <S::Target as _ComponentStoreIter<'a>>::Iter;

    type IterMut = <S::Target as _ComponentStoreIter<'a>>::IterMut;
}

impl<S> ComponentStoreIter for S
    where S: DerefMut + ServiceBase, S::Target: ComponentStoreIter
{
    fn iter<'a>(&'a self) -> <Self as _ComponentStoreIter<'a>>::Iter {
        (**self).iter()
    }

    fn iter_mut<'a>(&'a mut self) -> <Self as _ComponentStoreIter<'a>>::IterMut {
        (**self).iter_mut()
    }
}

impl<S> EntityStore for S
    where S: DerefMut + ServiceBase, S::Target: EntityStore
{
    fn assign<'id>(&mut self, id: Valid<'id, Self::Id>) -> bool {
        (**self).assign(id)
    }

    fn release<'id>(&mut self, id: Valid<'id, Self::Id>) -> bool {
        (**self).release(id)
    }
}

impl<'a, S> _EntityStoreIter<'a> for S
    where S: DerefMut + ServiceBase, S::Target: _EntityStoreIter<'a>
{
    type Iter = <S::Target as _EntityStoreIter<'a>>::Iter;
}

impl<S> EntityStoreIter for S
    where S: DerefMut + ServiceBase, S::Target: EntityStoreIter
{
    fn iter<'a>(&'a self) -> <Self as _EntityStoreIter<'a>>::Iter {
        (**self).iter()
    }
}

// ++++++++++++++++++++ StoreWithDirtyState ++++++++++++++++++++

impl<'a, S> _StoreWithDirtyState<'a> for S
    where S: DerefMut + ServiceBase, S::Target: _StoreWithDirtyState<'a>
{
    type DirtyEntities = <S::Target as _StoreWithDirtyState<'a>>::DirtyEntities;
}

impl<S> StoreWithDirtyState for S
    where S: DerefMut + ServiceBase, S::Target: StoreWithDirtyState
{
    fn was_cleared<'a>(&self) -> bool {
        (**self).was_cleared()
    }

    fn dirty_entities<'a>(&'a self) -> <Self as _StoreWithDirtyState<'a>>::DirtyEntities {
        (**self).dirty_entities()
    }
}