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
use super::{
    AbstractMut, CurrentId, DoubleEndedShiperator, ExactSizeShiperator, IntoAbstract, IntoIterator,
    Shiperator,
};
use crate::EntityId;

/// Update iterator over a single component.
pub struct Update1<T: IntoAbstract> {
    pub(super) data: T::AbsView,
    pub(super) current: usize,
    pub(super) end: usize,
    current_id: EntityId,
}

impl<T: IntoAbstract> Update1<T> {
    pub(crate) fn new(data: T) -> Self {
        Update1 {
            current: 0,
            end: data.len().unwrap_or(0),
            current_id: EntityId::dead(),
            data: data.into_abstract(),
        }
    }
}

impl<T: IntoAbstract> Shiperator for Update1<T> {
    type Item = <T::AbsView as AbstractMut>::Out;

    fn first_pass(&mut self) -> Option<Self::Item> {
        let current = self.current;
        if current < self.end {
            self.current += 1;
            // SAFE we checked for OOB
            self.current_id = unsafe { self.data.id_at(current) };
            // SAFE we checked for OOB and the lifetime is ok
            Some(unsafe { self.data.get_update_data(current) })
        } else {
            None
        }
    }
    fn post_process(&mut self) {
        // SAFE current_id has the component
        unsafe { self.data.flag(self.current_id) }
    }
    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.end - self.current;
        (len, Some(len))
    }
}

impl<T: IntoAbstract> CurrentId for Update1<T> {
    type Id = EntityId;

    unsafe fn current_id(&self) -> Self::Id {
        self.current_id
    }
}

impl<T: IntoAbstract> ExactSizeShiperator for Update1<T> {}

impl<T: IntoAbstract> DoubleEndedShiperator for Update1<T> {
    fn first_pass_back(&mut self) -> Option<Self::Item> {
        if self.current < self.end {
            self.end -= 1;
            // SAFE we checked for OOB
            self.current_id = unsafe { self.data.id_at(self.end) };
            // SAFE we checked for OOB and the lifetime is ok
            Some(unsafe { self.data.get_update_data(self.end) })
        } else {
            None
        }
    }
}

impl<I: IntoAbstract> core::iter::IntoIterator for Update1<I> {
    type IntoIter = IntoIterator<Self>;
    type Item = <Self as Shiperator>::Item;
    fn into_iter(self) -> Self::IntoIter {
        IntoIterator(self)
    }
}