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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use core::mem::MaybeUninit;
use crate::{
archetype::Archetype,
entity::{AliveEntity, EntityId, Location},
epoch::EpochCounter,
query::{AsQuery, ImmutableQuery, Query, QueryItem},
world::World,
};
use super::{expect_match, get_at, BorrowState, RuntimeBorrowState};
/// A view over [`World`] that may be used to access specific components
/// of one entity.
#[must_use]
pub struct ViewOneState<'a, Q: Query, F: Query> {
query: Q,
filter: F,
// Init if loc.arch != u32::MAX
archetype: MaybeUninit<&'a Archetype>,
id: EntityId,
loc: Location,
borrow: RuntimeBorrowState,
epochs: &'a EpochCounter,
}
impl<'a, Q: Query, F: Query> Drop for ViewOneState<'a, Q, F> {
#[inline(always)]
fn drop(&mut self) {
self.unlock()
}
}
impl<'a, Q: Query, F: Query> ViewOneState<'a, Q, F> {
/// Unlocks runtime borrows.
/// Allows usage of conflicting views.
///
/// Borrows are automatically unlocked when the view is dropped.
/// This method is necessary only if caller wants to keep the view
/// to reuse it later.
#[inline(always)]
pub fn unlock(&self) {
if self.loc.arch == u32::MAX {
return;
}
// Safety: archetype is init if loc.arch != u32::MAX
let archetype = unsafe { self.archetype.assume_init() };
self.borrow
.release(self.query, self.filter, core::slice::from_ref(archetype))
}
}
/// View for single entity.
pub type ViewOne<'a, Q, F = ()> = ViewOneState<'a, <Q as AsQuery>::Query, <F as AsQuery>::Query>;
impl<'a, Q, F> ViewOneState<'a, Q, F>
where
Q: Query,
F: Query,
{
/// Creates a new view over a single entity.
#[inline(always)]
pub fn new(world: &'a World, entity: impl AliveEntity, query: Q, filter: F) -> Self {
let loc = entity.locate(world.entities());
let mut archetype = MaybeUninit::uninit();
if loc.arch != u32::MAX {
archetype.write(&world.archetypes()[loc.arch as usize]);
}
ViewOneState {
query: query,
filter: filter,
archetype,
id: entity.id(),
loc,
borrow: RuntimeBorrowState::new(),
epochs: world.epoch_counter(),
}
}
}
impl<'a, Q, F> ViewOneState<'a, Q, F>
where
Q: Query,
F: Query,
{
/// Fetches data that matches the view's query and filter
/// from a bound entity.
///
/// Returns none if entity does not match the view's query and filter.
#[inline(always)]
pub fn get_mut(&mut self) -> Option<QueryItem<Q>> {
if self.loc.arch == u32::MAX {
return Query::reserved_entity_item(&self.query, self.id, self.loc.idx);
}
// Safety: archetype is init if loc.arch != u32::MAX
let archetype = unsafe { self.archetype.assume_init() };
// Ensure to borrow view's data.
self.borrow
.acquire(self.query, self.filter, core::slice::from_ref(archetype));
unsafe { get_at(self.query, self.filter, self.epochs, archetype, self.loc) }
}
/// Fetches data that matches the view's query and filter
/// from a bound entity.
///
/// # Panics
///
/// Panics if entity does not match the view's query and filter.
#[inline(always)]
#[track_caller]
pub fn expect_mut(&mut self) -> QueryItem<Q> {
expect_match(self.get_mut())
}
/// Fetches data that matches the view's query and filter
/// from a bound entity.
///
/// Calls provided closure with fetched data if entity matches query and filter.
/// Otherwise, returns `None`.
#[inline(always)]
pub fn map_mut<Fun, R>(&mut self, f: Fun) -> Option<R>
where
Fun: FnOnce(QueryItem<Q>) -> R,
{
if self.loc.arch == u32::MAX {
let item = Query::reserved_entity_item(&self.query, self.id, self.loc.idx);
return match item {
Some(item) => Some(f(item)),
None => None,
};
}
// Safety: archetype is init if loc.arch != u32::MAX
let archetype = unsafe { self.archetype.assume_init() };
// Ensure to borrow view's data.
self.borrow.with(self.query, self.filter, archetype, || {
let item = unsafe { get_at(self.query, self.filter, self.epochs, archetype, self.loc) };
match item {
Some(item) => Some(f(item)),
None => None,
}
})
}
}
impl<'a, Q, F> ViewOneState<'a, Q, F>
where
Q: ImmutableQuery,
F: ImmutableQuery,
{
/// Fetches data that matches the view's query and filter
/// from a bound entity.
///
/// Returns none if entity does not match the view's query and filter.
#[inline(always)]
pub fn get(&self) -> Option<QueryItem<Q>> {
if self.loc.arch == u32::MAX {
return Query::reserved_entity_item(&self.query, self.id, self.loc.idx);
}
// Safety: archetype is init if loc.arch != u32::MAX
let archetype = unsafe { self.archetype.assume_init() };
// Ensure to borrow view's data.
self.borrow
.acquire(self.query, self.filter, core::slice::from_ref(archetype));
unsafe { get_at(self.query, self.filter, self.epochs, archetype, self.loc) }
}
/// Fetches data that matches the view's query and filter
/// from a bound entity.
///
/// Returns none if entity does not match the view's query and filter.
#[inline(always)]
#[track_caller]
pub fn expect(&self) -> QueryItem<Q> {
expect_match(self.get())
}
/// Fetches data that matches the view's query and filter
/// from a bound entity.
///
/// Calls provided closure with fetched data if entity matches query and filter.
/// Otherwise, returns `None`.
#[inline(always)]
pub fn map<Fun, R>(&self, f: Fun) -> Option<R>
where
Fun: FnOnce(QueryItem<Q>) -> R,
{
if self.loc.arch == u32::MAX {
let item = Query::reserved_entity_item(&self.query, self.id, self.loc.idx);
return match item {
Some(item) => Some(f(item)),
None => None,
};
}
// Safety: archetype is init if loc.arch != u32::MAX
let archetype = unsafe { self.archetype.assume_init() };
// Ensure to borrow view's data.
self.borrow.with(self.query, self.filter, archetype, || {
let item = unsafe { get_at(self.query, self.filter, self.epochs, archetype, self.loc) };
match item {
Some(item) => Some(f(item)),
None => None,
}
})
}
}