//! A trait for components that let you traverse the ECS.12use crate::{3entity::Entity,4query::{ReadOnlyQueryData, ReleaseStateQueryData},5relationship::Relationship,6};78/// A component that can point to another entity, and which can be used to define a path through the ECS.9///10/// Traversals are used to [specify the direction] of [event propagation] in [observers].11/// The default query is `()`.12///13/// Infinite loops are possible, and are not checked for. While looping can be desirable in some contexts14/// (for example, an observer that triggers itself multiple times before stopping), following an infinite15/// traversal loop without an eventual exit will cause your application to hang. Each implementer of `Traversal`16/// is responsible for documenting possible looping behavior, and consumers of those implementations are responsible for17/// avoiding infinite loops in their code.18///19/// Traversals may be parameterized with additional data. For example, in observer event propagation, the20/// parameter `D` is the event type given in `On<E>`. This allows traversal to differ depending on event21/// data.22///23/// [specify the direction]: crate::event::EntityEvent::Traversal24/// [event propagation]: crate::observer::On::propagate25/// [observers]: crate::observer::Observer26pub trait Traversal<D: ?Sized>: ReadOnlyQueryData + ReleaseStateQueryData {27/// Returns the next entity to visit.28fn traverse(item: Self::Item<'_, '_>, data: &D) -> Option<Entity>;29}3031impl<D> Traversal<D> for () {32fn traverse(_: Self::Item<'_, '_>, _data: &D) -> Option<Entity> {33None34}35}3637/// This provides generalized hierarchy traversal for use in [event propagation].38///39/// # Warning40///41/// Traversing in a loop could result in infinite loops for relationship graphs with loops.42///43/// [event propagation]: crate::observer::On::propagate44impl<R: Relationship, D> Traversal<D> for &R {45fn traverse(item: Self::Item<'_, '_>, _data: &D) -> Option<Entity> {46Some(item.get())47}48}495051