bevy_ecs/
traversal.rs

1//! A trait for components that let you traverse the ECS.
2
3use crate::{entity::Entity, query::ReadOnlyQueryData};
4
5/// A component that can point to another entity, and which can be used to define a path through the ECS.
6///
7/// Traversals are used to [specify the direction] of [event propagation] in [observers].
8/// The default query is `()`.
9///
10/// Infinite loops are possible, and are not checked for. While looping can be desirable in some contexts
11/// (for example, an observer that triggers itself multiple times before stopping), following an infinite
12/// traversal loop without an eventual exit will can your application to hang. Each implementer of `Traversal`
13/// for documenting possible looping behavior, and consumers of those implementations are responsible for
14/// avoiding infinite loops in their code.
15///
16/// [specify the direction]: crate::event::Event::Traversal
17/// [event propagation]: crate::observer::Trigger::propagate
18/// [observers]: crate::observer::Observer
19pub trait Traversal: ReadOnlyQueryData {
20    /// Returns the next entity to visit.
21    fn traverse(item: Self::Item<'_>) -> Option<Entity>;
22}
23
24impl Traversal for () {
25    fn traverse(_: Self::Item<'_>) -> Option<Entity> {
26        None
27    }
28}