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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
use crate::{
archetype::Archetype,
change_detection::{ComponentTicks, MaybeLocation, Tick},
component::{Component, ComponentId},
entity::{ContainsEntity, Entity, EntityEquivalent, EntityLocation},
query::{
Access, QueryAccessError, ReadOnlyQueryData, ReleaseStateQueryData, SingleEntityQueryData,
},
world::{
error::EntityComponentError, unsafe_world_cell::UnsafeEntityCell, DynamicComponentFetch,
FilteredEntityRef, Ref,
},
};
use core::{
any::TypeId,
cmp::Ordering,
hash::{Hash, Hasher},
};
/// A read-only reference to a particular [`Entity`] and all of its components.
///
/// # Examples
///
/// Read-only access disjoint with mutable access.
///
/// ```
/// # use bevy_ecs::prelude::*;
/// # #[derive(Component)] pub struct A;
/// # #[derive(Component)] pub struct B;
/// fn disjoint_system(
/// query1: Query<&mut A>,
/// query2: Query<EntityRef, Without<A>>,
/// ) {
/// // ...
/// }
/// # bevy_ecs::system::assert_is_system(disjoint_system);
/// ```
#[derive(Copy, Clone)]
pub struct EntityRef<'w> {
cell: UnsafeEntityCell<'w>,
}
impl<'w> EntityRef<'w> {
/// # Safety
/// - `cell` must have permission to read every component of the entity.
/// - No mutable accesses to any of the entity's components may exist
/// at the same time as the returned [`EntityRef`].
#[inline]
pub(crate) unsafe fn new(cell: UnsafeEntityCell<'w>) -> Self {
Self { cell }
}
/// Consumes `self` and returns a [`FilteredEntityRef`] which has read-only
/// access to all of the entity's components, with the world `'w` lifetime.
#[inline]
pub fn into_filtered(self) -> FilteredEntityRef<'w, 'static> {
// SAFETY:
// - `EntityRef` guarantees exclusive access to all components in the new `FilteredEntityRef`.
unsafe { FilteredEntityRef::new(self.cell, const { &Access::new_read_all() }) }
}
/// Returns the [ID](Entity) of the current entity.
#[inline]
#[must_use = "Omit the .id() call if you do not need to store the `Entity` identifier."]
pub fn id(&self) -> Entity {
self.cell.id()
}
/// Gets metadata indicating the location where the current entity is stored.
#[inline]
pub fn location(&self) -> EntityLocation {
self.cell.location()
}
/// Returns the archetype that the current entity belongs to.
#[inline]
pub fn archetype(&self) -> &Archetype {
self.cell.archetype()
}
/// Returns `true` if the current entity has a component of type `T`.
/// Otherwise, this returns `false`.
///
/// ## Notes
///
/// If you do not know the concrete type of a component, consider using
/// [`Self::contains_id`] or [`Self::contains_type_id`].
#[inline]
pub fn contains<T: Component>(&self) -> bool {
self.contains_type_id(TypeId::of::<T>())
}
/// Returns `true` if the current entity has a component identified by `component_id`.
/// Otherwise, this returns false.
///
/// ## Notes
///
/// - If you know the concrete type of the component, you should prefer [`Self::contains`].
/// - If you know the component's [`TypeId`] but not its [`ComponentId`], consider using
/// [`Self::contains_type_id`].
#[inline]
pub fn contains_id(&self, component_id: ComponentId) -> bool {
self.cell.contains_id(component_id)
}
/// Returns `true` if the current entity has a component with the type identified by `type_id`.
/// Otherwise, this returns false.
///
/// ## Notes
///
/// - If you know the concrete type of the component, you should prefer [`Self::contains`].
/// - If you have a [`ComponentId`] instead of a [`TypeId`], consider using [`Self::contains_id`].
#[inline]
pub fn contains_type_id(&self, type_id: TypeId) -> bool {
self.cell.contains_type_id(type_id)
}
/// Gets access to the component of type `T` for the current entity.
/// Returns `None` if the entity does not have a component of type `T`.
#[inline]
pub fn get<T: Component>(&self) -> Option<&'w T> {
// SAFETY: We have read-only access to all components of this entity.
unsafe { self.cell.get::<T>() }
}
/// Gets access to the component of type `T` for the current entity,
/// including change detection information as a [`Ref`].
///
/// Returns `None` if the entity does not have a component of type `T`.
#[inline]
pub fn get_ref<T: Component>(&self) -> Option<Ref<'w, T>> {
// SAFETY: We have read-only access to all components of this entity.
unsafe { self.cell.get_ref::<T>() }
}
/// Retrieves the change ticks for the given component. This can be useful for implementing change
/// detection in custom runtimes.
#[inline]
pub fn get_change_ticks<T: Component>(&self) -> Option<ComponentTicks> {
// SAFETY: We have read-only access to all components of this entity.
unsafe { self.cell.get_change_ticks::<T>() }
}
/// Get the [`MaybeLocation`] from where the given [`Component`] was last changed from.
/// This contains information regarding the last place (in code) that changed this component and can be useful for debugging.
/// For more information, see [`Location`](https://doc.rust-lang.org/nightly/core/panic/struct.Location.html), and enable the `track_location` feature.
pub fn get_changed_by<T: Component>(&self) -> Option<MaybeLocation> {
// SAFETY: We have read-only access to all components of this entity.
unsafe { self.cell.get_changed_by::<T>() }
}
/// Retrieves the change ticks for the given [`ComponentId`]. This can be useful for implementing change
/// detection in custom runtimes.
///
/// **You should prefer to use the typed API [`EntityRef::get_change_ticks`] where possible and only
/// use this in cases where the actual component types are not known at
/// compile time.**
#[inline]
pub fn get_change_ticks_by_id(&self, component_id: ComponentId) -> Option<ComponentTicks> {
// SAFETY: We have read-only access to all components of this entity.
unsafe { self.cell.get_change_ticks_by_id(component_id) }
}
/// Returns untyped read-only reference(s) to component(s) for the
/// current entity, based on the given [`ComponentId`]s.
///
/// **You should prefer to use the typed API [`EntityRef::get`] where
/// possible and only use this in cases where the actual component types
/// are not known at compile time.**
///
/// Unlike [`EntityRef::get`], this returns untyped reference(s) to
/// component(s), and it's the job of the caller to ensure the correct
/// type(s) are dereferenced (if necessary).
///
/// # Errors
///
/// Returns [`EntityComponentError::MissingComponent`] if the entity does
/// not have a component.
///
/// # Examples
///
/// ## Single [`ComponentId`]
///
/// ```
/// # use bevy_ecs::prelude::*;
/// #
/// # #[derive(Component, PartialEq, Debug)]
/// # pub struct Foo(i32);
/// # let mut world = World::new();
/// let entity = world.spawn(Foo(42)).id();
///
/// // Grab the component ID for `Foo` in whatever way you like.
/// let component_id = world.register_component::<Foo>();
///
/// // Then, get the component by ID.
/// let ptr = world.entity(entity).get_by_id(component_id);
/// # assert_eq!(unsafe { ptr.unwrap().deref::<Foo>() }, &Foo(42));
/// ```
///
/// ## Array of [`ComponentId`]s
///
/// ```
/// # use bevy_ecs::prelude::*;
/// #
/// # #[derive(Component, PartialEq, Debug)]
/// # pub struct X(i32);
/// # #[derive(Component, PartialEq, Debug)]
/// # pub struct Y(i32);
/// # let mut world = World::new();
/// let entity = world.spawn((X(42), Y(10))).id();
///
/// // Grab the component IDs for `X` and `Y` in whatever way you like.
/// let x_id = world.register_component::<X>();
/// let y_id = world.register_component::<Y>();
///
/// // Then, get the components by ID. You'll receive a same-sized array.
/// let Ok([x_ptr, y_ptr]) = world.entity(entity).get_by_id([x_id, y_id]) else {
/// // Up to you to handle if a component is missing from the entity.
/// # unreachable!();
/// };
/// # assert_eq!((unsafe { x_ptr.deref::<X>() }, unsafe { y_ptr.deref::<Y>() }), (&X(42), &Y(10)));
/// ```
///
/// ## Slice of [`ComponentId`]s
///
/// ```
/// # use bevy_ecs::{prelude::*, component::ComponentId};
/// #
/// # #[derive(Component, PartialEq, Debug)]
/// # pub struct X(i32);
/// # #[derive(Component, PartialEq, Debug)]
/// # pub struct Y(i32);
/// # let mut world = World::new();
/// let entity = world.spawn((X(42), Y(10))).id();
///
/// // Grab the component IDs for `X` and `Y` in whatever way you like.
/// let x_id = world.register_component::<X>();
/// let y_id = world.register_component::<Y>();
///
/// // Then, get the components by ID. You'll receive a vec of ptrs.
/// let ptrs = world.entity(entity).get_by_id(&[x_id, y_id] as &[ComponentId]);
/// # let ptrs = ptrs.unwrap();
/// # assert_eq!((unsafe { ptrs[0].deref::<X>() }, unsafe { ptrs[1].deref::<Y>() }), (&X(42), &Y(10)));
/// ```
///
/// ## `HashSet` of [`ComponentId`]s
///
/// ```
/// # use bevy_platform::collections::HashSet;
/// # use bevy_ecs::{prelude::*, component::ComponentId};
/// #
/// # #[derive(Component, PartialEq, Debug)]
/// # pub struct X(i32);
/// # #[derive(Component, PartialEq, Debug)]
/// # pub struct Y(i32);
/// # let mut world = World::new();
/// let entity = world.spawn((X(42), Y(10))).id();
///
/// // Grab the component IDs for `X` and `Y` in whatever way you like.
/// let x_id = world.register_component::<X>();
/// let y_id = world.register_component::<Y>();
///
/// // Then, get the components by ID. You'll receive a vec of ptrs.
/// let ptrs = world.entity(entity).get_by_id(&HashSet::from_iter([x_id, y_id]));
/// # let ptrs = ptrs.unwrap();
/// # assert_eq!((unsafe { ptrs[&x_id].deref::<X>() }, unsafe { ptrs[&y_id].deref::<Y>() }), (&X(42), &Y(10)));
/// ```
#[inline]
pub fn get_by_id<F: DynamicComponentFetch>(
&self,
component_ids: F,
) -> Result<F::Ref<'w>, EntityComponentError> {
// SAFETY: We have read-only access to all components of this entity.
unsafe { component_ids.fetch_ref(self.cell) }
}
/// Returns read-only components for the current entity that match the query `Q`.
///
/// # Panics
///
/// If the entity does not have the components required by the query `Q`.
pub fn components<Q: ReadOnlyQueryData + ReleaseStateQueryData + SingleEntityQueryData>(
&self,
) -> Q::Item<'w, 'static> {
self.get_components::<Q>()
.expect("Query does not match the current entity")
}
/// Returns read-only components for the current entity that match the query `Q`,
/// or `None` if the entity does not have the components required by the query `Q`.
pub fn get_components<Q: ReadOnlyQueryData + ReleaseStateQueryData + SingleEntityQueryData>(
&self,
) -> Result<Q::Item<'w, 'static>, QueryAccessError> {
// SAFETY:
// - We have read-only access to all components of this entity.
// - The query is read-only, and read-only references cannot have conflicts.
unsafe { self.cell.get_components::<Q>() }
}
/// Returns the source code location from which this entity has been spawned.
pub fn spawned_by(&self) -> MaybeLocation {
self.cell.spawned_by()
}
/// Returns the [`Tick`] at which this entity has been spawned.
pub fn spawn_tick(&self) -> Tick {
self.cell.spawn_tick()
}
}
impl<'a> From<EntityRef<'a>> for FilteredEntityRef<'a, 'static> {
#[inline]
fn from(entity: EntityRef<'a>) -> Self {
entity.into_filtered()
}
}
impl<'a> From<&EntityRef<'a>> for FilteredEntityRef<'a, 'static> {
#[inline]
fn from(entity: &EntityRef<'a>) -> Self {
entity.into_filtered()
}
}
impl PartialEq for EntityRef<'_> {
fn eq(&self, other: &Self) -> bool {
self.entity() == other.entity()
}
}
impl Eq for EntityRef<'_> {}
impl PartialOrd for EntityRef<'_> {
/// [`EntityRef`]'s comparison trait implementations match the underlying [`Entity`],
/// and cannot discern between different worlds.
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for EntityRef<'_> {
fn cmp(&self, other: &Self) -> Ordering {
self.entity().cmp(&other.entity())
}
}
impl Hash for EntityRef<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.entity().hash(state);
}
}
impl ContainsEntity for EntityRef<'_> {
fn entity(&self) -> Entity {
self.id()
}
}
// SAFETY: This type represents one Entity. We implement the comparison traits based on that Entity.
unsafe impl EntityEquivalent for EntityRef<'_> {}