-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Event source location tracking (#16778)
# Objective Fixes #16776 ## Solution - reflect `&'static Location` as an opaque type - I've added this to `impls/std.rs` because other core types are there too. Maybe they should be split out into a `core.rs` in another PR. - add source location to `EventId` (behind the `tracking_change_detection` feature flag) ## Testing --- ## Showcase ```rust fn apply_damage_to_health( mut dmg_events: EventReader<DealDamage>, ) { for (event, event_id) in dmg_events.read_with_id() { info!( "Applying {} damage, triggered by {}", event.amount, event_id.caller ); … ``` ``` 2024-12-12T01:21:50.126827Z INFO event: Applying 9 damage, triggered by examples/ecs/event.rs:47:16 ``` ## Migration Guide - If you manually construct a `SendEvent`, use `SendEvent::new()` --------- Co-authored-by: Alice Cecile <[email protected]>
- Loading branch information
1 parent
aa51959
commit b2d3371
Showing
6 changed files
with
205 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,37 @@ | ||
#[cfg(feature = "track_change_detection")] | ||
use core::panic::Location; | ||
|
||
use super::{Event, Events}; | ||
use crate::world::{Command, World}; | ||
|
||
/// A command to send an arbitrary [`Event`], used by [`Commands::send_event`](crate::system::Commands::send_event). | ||
pub struct SendEvent<E: Event> { | ||
/// The event to send. | ||
pub event: E, | ||
/// The source code location that triggered this command. | ||
#[cfg(feature = "track_change_detection")] | ||
pub caller: &'static Location<'static>, | ||
} | ||
|
||
// This does not use `From`, as the resulting `Into` is not track_caller | ||
impl<E: Event> SendEvent<E> { | ||
/// Constructs a new `SendEvent` tracking the caller. | ||
pub fn new(event: E) -> Self { | ||
Self { | ||
event, | ||
#[cfg(feature = "track_change_detection")] | ||
caller: Location::caller(), | ||
} | ||
} | ||
} | ||
|
||
impl<E: Event> Command for SendEvent<E> { | ||
fn apply(self, world: &mut World) { | ||
let mut events = world.resource_mut::<Events<E>>(); | ||
events.send(self.event); | ||
events.send_with_caller( | ||
self.event, | ||
#[cfg(feature = "track_change_detection")] | ||
self.caller, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters