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
use crate::entity_id::EntityId;
use core::iter::Map;
#[allow(missing_docs)]
pub struct WithId<I>(pub I);
pub trait IntoWithId {
fn with_id(self) -> WithId<Self>
where
Self: Sized;
#[allow(clippy::type_complexity)]
fn ids(self) -> Map<WithId<Self>, fn(<WithId<Self> as Iterator>::Item) -> EntityId>
where
Self: Sized + Iterator,
WithId<Self>: Iterator<Item = (EntityId, <Self as Iterator>::Item)>;
}
impl<I> IntoWithId for I
where
I: Iterator,
WithId<I>: Iterator<Item = (EntityId, <Self as Iterator>::Item)>,
{
fn with_id(self) -> WithId<Self> {
WithId(self)
}
#[allow(clippy::type_complexity)]
fn ids(self) -> Map<WithId<Self>, fn(<WithId<Self> as Iterator>::Item) -> EntityId> {
self.with_id().map(|(id, _)| id)
}
}
pub trait LastId {
unsafe fn last_id(&self) -> EntityId;
unsafe fn last_id_back(&self) -> EntityId;
}
impl<I: Iterator + LastId> Iterator for WithId<I> {
type Item = (EntityId, <I as Iterator>::Item);
fn next(&mut self) -> Option<Self::Item> {
let item = self.0.next()?;
Some((unsafe { self.0.last_id() }, item))
}
}