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
use std::marker::PhantomData;
use std::ops::Deref;
use std::{mem, slice};

use specs::Entity;

#[derive(Debug)]
pub struct SpriteGuide<T> {
    sorted: Vec<Entity>,
    _marker: PhantomData<T>,
}

impl<T> Default for SpriteGuide<T> {
    #[inline(always)]
    fn default() -> Self {
        Self::new()
    }
}

impl<T> SpriteGuide<T> {
    #[inline(always)]
    pub fn new() -> Self {
        SpriteGuide {
            sorted: Vec::new(),
            _marker: PhantomData,
        }
    }

    #[inline]
    pub(crate) fn swap(&mut self, sorted: &mut Vec<Entity>) {
        mem::swap(&mut self.sorted, sorted);
        self.sorted.shrink_to_fit();
    }

    #[inline(always)]
    pub fn as_slice(&self) -> &[Entity] {
        &*self.sorted
    }
    #[inline(always)]
    pub(crate) fn as_slice_mut(&mut self) -> &mut [Entity] {
        &mut *self.sorted
    }
}

impl<T> Deref for SpriteGuide<T> {
    type Target = [Entity];

    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        self.as_slice()
    }
}

impl<'a, T> IntoIterator for &'a SpriteGuide<T> {
    type Item = &'a Entity;
    type IntoIter = slice::Iter<'a, Entity>;

    fn into_iter(self) -> Self::IntoIter {
        self.as_slice().into_iter()
    }
}