Path: blob/main/crates/bevy_ecs/src/system/observer_system.rs
6604 views
use crate::{1event::Event,2prelude::{Bundle, On},3system::System,4};56use super::IntoSystem;78/// Implemented for [`System`]s that have [`On`] as the first argument.9pub trait ObserverSystem<E: Event, B: Bundle, Out = ()>:10System<In = On<'static, 'static, E, B>, Out = Out> + Send + 'static11{12}1314impl<E: Event, B: Bundle, Out, T> ObserverSystem<E, B, Out> for T where15T: System<In = On<'static, 'static, E, B>, Out = Out> + Send + 'static16{17}1819/// Implemented for systems that convert into [`ObserverSystem`].20///21/// # Usage notes22///23/// This trait should only be used as a bound for trait implementations or as an24/// argument to a function. If an observer system needs to be returned from a25/// function or stored somewhere, use [`ObserverSystem`] instead of this trait.26#[diagnostic::on_unimplemented(27message = "`{Self}` cannot become an `ObserverSystem`",28label = "the trait `IntoObserverSystem` is not implemented",29note = "for function `ObserverSystem`s, ensure the first argument is `On<T>` and any subsequent ones are `SystemParam`"30)]31pub trait IntoObserverSystem<E: Event, B: Bundle, M, Out = ()>: Send + 'static {32/// The type of [`System`] that this instance converts into.33type System: ObserverSystem<E, B, Out>;3435/// Turns this value into its corresponding [`System`].36fn into_system(this: Self) -> Self::System;37}3839impl<E: Event, B, M, Out, S> IntoObserverSystem<E, B, M, Out> for S40where41S: IntoSystem<On<'static, 'static, E, B>, Out, M> + Send + 'static,42S::System: ObserverSystem<E, B, Out>,43E: 'static,44B: Bundle,45{46type System = S::System;4748fn into_system(this: Self) -> Self::System {49IntoSystem::into_system(this)50}51}5253#[cfg(test)]54mod tests {55use crate::{56event::Event,57observer::On,58system::{In, IntoSystem},59world::World,60};6162#[derive(Event)]63struct TriggerEvent;6465#[test]66fn test_piped_observer_systems_no_input() {67fn a(_: On<TriggerEvent>) {}68fn b() {}6970let mut world = World::new();71world.add_observer(a.pipe(b));72}7374#[test]75fn test_piped_observer_systems_with_inputs() {76fn a(_: On<TriggerEvent>) -> u32 {77378}79fn b(_: In<u32>) {}8081let mut world = World::new();82world.add_observer(a.pipe(b));83}84}858687