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
use core::marker::PhantomData;
use crate::{
query::{Fetch, ReadOnlyFetch, With, Without},
Archetype, Component, Query,
};
pub struct QueryOne<'a, Q: Query> {
archetype: &'a Archetype,
index: usize,
_marker: PhantomData<Q>,
}
impl<'a, Q: Query> QueryOne<'a, Q> {
pub(crate) unsafe fn new(archetype: &'a Archetype, index: usize) -> Self {
Self {
archetype,
index,
_marker: PhantomData,
}
}
pub fn get(&mut self) -> Option<<Q::Fetch as Fetch<'_>>::Item> {
unsafe {
let mut fetch = Q::Fetch::get(self.archetype, self.index)?;
Some(fetch.next())
}
}
pub fn with<T: Component>(self) -> QueryOne<'a, With<T, Q>> {
self.transform()
}
pub fn without<T: Component>(self) -> QueryOne<'a, Without<T, Q>> {
self.transform()
}
fn transform<R: Query>(self) -> QueryOne<'a, R> {
QueryOne {
archetype: self.archetype,
index: self.index,
_marker: PhantomData,
}
}
}
unsafe impl<Q: Query> Send for QueryOne<'_, Q> {}
unsafe impl<Q: Query> Sync for QueryOne<'_, Q> {}
pub struct ReadOnlyQueryOne<'a, Q: Query> {
archetype: &'a Archetype,
index: usize,
_marker: PhantomData<Q>,
}
impl<'a, Q: Query> ReadOnlyQueryOne<'a, Q>
where
Q::Fetch: ReadOnlyFetch,
{
pub(crate) unsafe fn new(archetype: &'a Archetype, index: usize) -> Self {
Self {
archetype,
index,
_marker: PhantomData,
}
}
pub fn get(&self) -> Option<<Q::Fetch as Fetch<'_>>::Item>
where
Q::Fetch: ReadOnlyFetch,
{
unsafe {
let mut fetch = Q::Fetch::get(self.archetype, self.index)?;
Some(fetch.next())
}
}
pub fn with<T: Component>(self) -> QueryOne<'a, With<T, Q>> {
self.transform()
}
pub fn without<T: Component>(self) -> QueryOne<'a, Without<T, Q>> {
self.transform()
}
fn transform<R: Query>(self) -> QueryOne<'a, R> {
QueryOne {
archetype: self.archetype,
index: self.index,
_marker: PhantomData,
}
}
}
unsafe impl<Q: Query> Send for ReadOnlyQueryOne<'_, Q> {}
unsafe impl<Q: Query> Sync for ReadOnlyQueryOne<'_, Q> {}