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
use std::slice;
use crate::{
    Bitmask, Entity, MaskType,
    entity::{EntitiesStorage},
    storage::{IntoSendStorage, FastIndexExt, ReadOnlyStorage, StorageRef}
};
use super::{DataAccesses, FromComponent, SafeIter, UnorderedData};
use std::usize;
use std::any::TypeId;
#[cfg(components_64)]
use num_traits::Zero;

impl<'a> FromComponent<'a, Entity> for Entity{
    fn from_component(entity: Entity) -> Entity{
        entity
    }
}

impl<'a> FastIndexExt for &'a [(Entity, MaskType)] {
    type FastIndex = usize;
    type StaticTypeId = Entity;

    fn fast_index(&self, guid: usize) -> usize {
        guid
    }
}

impl<'r, 'e> StorageRef<'r> for &'e [(Entity, MaskType)]{
    type Data = Entity;
    type Component = Entity;

    unsafe fn get_fast_unchecked(&'r mut self, idx: usize) -> Entity{
        (**self).get_unchecked(idx).0
    }

    unsafe fn get_unchecked(&'r mut self, guid: usize) -> Entity{
        (**self).get_unchecked(guid).0
    }

    fn contains(&self, guid: usize) -> bool{
        (**self).get(guid).map(|(e,_)| e.guid() == guid).unwrap_or(false)
    }
}

impl<'r, 'e> IntoSendStorage<'r> for &'e [(Entity, MaskType)] {
    type SendStorage = &'r [(Entity, MaskType)];

    fn into_send_storage(&'r mut self) -> Self::SendStorage {
        self
    }
}

unsafe impl<'s> ReadOnlyStorage for &'s [(Entity, MaskType)]{}

pub struct EntitiesIter<'a>(slice::Iter<'a, (Entity, MaskType)>);

impl<'a> Iterator for EntitiesIter<'a>{
    type Item = Entity;
    fn next(&mut self) -> Option<Entity>{
        while let Some(e) = self.0.next().map(|&(e, _)| e){
            if e.guid() != usize::MAX {
                return Some(e)
            }
        }
        None
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (0, self.0.size_hint().1)
    }
}

unsafe impl SafeIter<'_> for Entity{}

impl<'a> UnorderedData<'a> for Entity {
    type Iter = EntitiesIter<'a>;
    type IterMut = EntitiesIter<'a>;
    type Components = Entity;
    type ComponentsRef = Entity;
    type Storage = &'a [(Entity, MaskType)];

    fn query_mask<E: EntitiesStorage>(_entities: &E) -> Bitmask{
        Bitmask::all()
    }

    fn into_iter<E: EntitiesStorage>(entities: &'a E) -> Self::Iter{
        EntitiesIter(entities.entities_ref().iter())
    }

    fn into_iter_mut<E: EntitiesStorage>(entities: &'a E) -> Self::Iter {
        Self::into_iter(entities)
    }

    fn storage<E: EntitiesStorage>(entities: &'a E) -> Option<Self::Storage>{
        Some(entities.entities_ref())
    }
}

impl DataAccesses for Entity {
    fn reads() -> Vec<TypeId>{ vec![] }
    fn writes() -> Vec<TypeId>{ vec![] }
}