Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_ecs/src/traversal.rs
6598 views
1
//! A trait for components that let you traverse the ECS.
2
3
use crate::{
4
entity::Entity,
5
query::{ReadOnlyQueryData, ReleaseStateQueryData},
6
relationship::Relationship,
7
};
8
9
/// A component that can point to another entity, and which can be used to define a path through the ECS.
10
///
11
/// Traversals are used to [specify the direction] of [event propagation] in [observers].
12
/// The default query is `()`.
13
///
14
/// Infinite loops are possible, and are not checked for. While looping can be desirable in some contexts
15
/// (for example, an observer that triggers itself multiple times before stopping), following an infinite
16
/// traversal loop without an eventual exit will cause your application to hang. Each implementer of `Traversal`
17
/// is responsible for documenting possible looping behavior, and consumers of those implementations are responsible for
18
/// avoiding infinite loops in their code.
19
///
20
/// Traversals may be parameterized with additional data. For example, in observer event propagation, the
21
/// parameter `D` is the event type given in `On<E>`. This allows traversal to differ depending on event
22
/// data.
23
///
24
/// [specify the direction]: crate::event::EntityEvent::Traversal
25
/// [event propagation]: crate::observer::On::propagate
26
/// [observers]: crate::observer::Observer
27
pub trait Traversal<D: ?Sized>: ReadOnlyQueryData + ReleaseStateQueryData {
28
/// Returns the next entity to visit.
29
fn traverse(item: Self::Item<'_, '_>, data: &D) -> Option<Entity>;
30
}
31
32
impl<D> Traversal<D> for () {
33
fn traverse(_: Self::Item<'_, '_>, _data: &D) -> Option<Entity> {
34
None
35
}
36
}
37
38
/// This provides generalized hierarchy traversal for use in [event propagation].
39
///
40
/// # Warning
41
///
42
/// Traversing in a loop could result in infinite loops for relationship graphs with loops.
43
///
44
/// [event propagation]: crate::observer::On::propagate
45
impl<R: Relationship, D> Traversal<D> for &R {
46
fn traverse(item: Self::Item<'_, '_>, _data: &D) -> Option<Entity> {
47
Some(item.get())
48
}
49
}
50
51