Skip to content

Commit 713acc2

Browse files
SteveAlexanderSteve Alexander
andauthored
Change event to event_key where it refers to an EventKey (#20060)
# Objective The ECS code to do with Observers was recently refactored to use an `EventKey` newtype. On reading through the PR, I was a bit confused that sometimes a variable called `event` refers to an `Event` and sometimes it refers to an `EventKey`. I think the code is clearer when `event` refers to an `Event` and `event_key` refers to an `EventKey`. ## Solution This PR renames some uses of `event` to `event_key`. ## Testing - Did you test these changes? If so, how? I ran `cargo run -p ci -- tests` - Are there any parts that need more testing? No - How can other people (reviewers) test your changes? Is there anything specific they need to know? No. - If relevant, what platforms did you test these changes on, and are there any important ones you can't test? Probably not relevant, but MacOS. --------- Co-authored-by: Steve Alexander <[email protected]>
1 parent 2bcd854 commit 713acc2

File tree

6 files changed

+29
-29
lines changed

6 files changed

+29
-29
lines changed

crates/bevy_ecs/src/event/registry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct RegisteredEvent {
1212
event_key: EventKey,
1313
// Required to flush the secondary buffer and drop events even if left unchanged.
1414
previously_updated: bool,
15-
// SAFETY: The component ID and the function must be used to fetch the Events<T> resource
15+
// SAFETY: The `EventKey`'s component ID and the function must be used to fetch the Events<T> resource
1616
// of the same type initialized in `register_event`, or improper type casts will occur.
1717
update: unsafe fn(MutUntyped),
1818
}

crates/bevy_ecs/src/observer/distributed_storage.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl Observer {
248248
world.commands().queue(move |world: &mut World| {
249249
let entity = hook_context.entity;
250250
if let Some(mut observe) = world.get_mut::<Observer>(entity) {
251-
if observe.descriptor.events.is_empty() {
251+
if observe.descriptor.event_keys.is_empty() {
252252
return;
253253
}
254254
if observe.error_handler.is_none() {
@@ -286,13 +286,13 @@ impl Observer {
286286
self
287287
}
288288

289-
/// Observe the given `event`. This will cause the [`Observer`] to run whenever an event with the given [`ComponentId`]
289+
/// Observe the given `event_key`. This will cause the [`Observer`] to run whenever an event with the given [`EventKey`]
290290
/// is triggered.
291291
/// # Safety
292-
/// The type of the `event` [`EventKey`] _must_ match the actual value
292+
/// The type of the `event_key` [`EventKey`] _must_ match the actual value
293293
/// of the event passed into the observer system.
294-
pub unsafe fn with_event(mut self, event: EventKey) -> Self {
295-
self.descriptor.events.push(event);
294+
pub unsafe fn with_event_key(mut self, event_key: EventKey) -> Self {
295+
self.descriptor.event_keys.push(event_key);
296296
self
297297
}
298298

@@ -349,8 +349,8 @@ impl Component for Observer {
349349
/// This information is stored inside of the [`Observer`] component,
350350
#[derive(Default, Clone)]
351351
pub struct ObserverDescriptor {
352-
/// The events the observer is watching.
353-
pub(super) events: Vec<EventKey>,
352+
/// The event keys the observer is watching.
353+
pub(super) event_keys: Vec<EventKey>,
354354

355355
/// The components the observer is watching.
356356
pub(super) components: Vec<ComponentId>,
@@ -360,12 +360,12 @@ pub struct ObserverDescriptor {
360360
}
361361

362362
impl ObserverDescriptor {
363-
/// Add the given `events` to the descriptor.
363+
/// Add the given `event_keys` to the descriptor.
364364
/// # Safety
365-
/// The type of each [`EventKey`] in `events` _must_ match the actual value
365+
/// The type of each [`EventKey`] in `event_keys` _must_ match the actual value
366366
/// of the event passed into the observer.
367-
pub unsafe fn with_events(mut self, events: Vec<EventKey>) -> Self {
368-
self.events = events;
367+
pub unsafe fn with_event_keys(mut self, event_keys: Vec<EventKey>) -> Self {
368+
self.event_keys = event_keys;
369369
self
370370
}
371371

@@ -381,9 +381,9 @@ impl ObserverDescriptor {
381381
self
382382
}
383383

384-
/// Returns the `events` that the observer is watching.
385-
pub fn events(&self) -> &[EventKey] {
386-
&self.events
384+
/// Returns the `event_keys` that the observer is watching.
385+
pub fn event_keys(&self) -> &[EventKey] {
386+
&self.event_keys
387387
}
388388

389389
/// Returns the `components` that the observer is watching.
@@ -416,7 +416,7 @@ fn hook_on_add<E: Event, B: Bundle, S: ObserverSystem<E, B>>(
416416
components.push(id);
417417
});
418418
if let Some(mut observer) = world.get_mut::<Observer>(entity) {
419-
observer.descriptor.events.push(event_key);
419+
observer.descriptor.event_keys.push(event_key);
420420
observer.descriptor.components.extend(components);
421421

422422
let system: &mut dyn Any = observer.system.as_mut();

crates/bevy_ecs/src/observer/entity_cloning.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn component_clone_observed_by(_source: &SourceComponent, ctx: &mut ComponentClo
4343
.get_mut::<Observer>(observer_entity)
4444
.expect("Source observer entity must have Observer");
4545
observer_state.descriptor.entities.push(target);
46-
let event_keys = observer_state.descriptor.events.clone();
46+
let event_keys = observer_state.descriptor.event_keys.clone();
4747
let components = observer_state.descriptor.components.clone();
4848
for event_key in event_keys {
4949
let observers = world.observers.get_observers_mut(event_key);

crates/bevy_ecs/src/observer/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ impl World {
379379
};
380380
let descriptor = &observer_state.descriptor;
381381

382-
for &event_key in &descriptor.events {
382+
for &event_key in &descriptor.event_keys {
383383
let cache = observers.get_observers_mut(event_key);
384384

385385
if descriptor.components.is_empty() && descriptor.entities.is_empty() {
@@ -430,7 +430,7 @@ impl World {
430430
let archetypes = &mut self.archetypes;
431431
let observers = &mut self.observers;
432432

433-
for &event_key in &descriptor.events {
433+
for &event_key in &descriptor.event_keys {
434434
let cache = observers.get_observers_mut(event_key);
435435
if descriptor.components.is_empty() && descriptor.entities.is_empty() {
436436
cache.global_observers.remove(&entity);
@@ -741,7 +741,7 @@ mod tests {
741741
Observer::new(|_: On<Add, A>, mut res: ResMut<Order>| {
742742
res.observed("add/remove");
743743
})
744-
.with_event(on_remove)
744+
.with_event_key(on_remove)
745745
},
746746
);
747747

@@ -1015,7 +1015,7 @@ mod tests {
10151015
Observer::with_dynamic_runner(|mut world, _trigger, _ptr, _propagate| {
10161016
world.resource_mut::<Order>().observed("event_a");
10171017
})
1018-
.with_event(event_a)
1018+
.with_event_key(event_a)
10191019
};
10201020
world.spawn(observe);
10211021

crates/bevy_ecs/src/observer/system_param.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl<'w, E, B: Bundle> DerefMut for On<'w, E, B> {
181181
pub struct ObserverTrigger {
182182
/// The [`Entity`] of the observer handling the trigger.
183183
pub observer: Entity,
184-
/// The [`Event`] the trigger targeted.
184+
/// The [`EventKey`] the trigger targeted.
185185
pub event_key: EventKey,
186186
/// The [`ComponentId`]s the trigger targeted.
187187
pub components: SmallVec<[ComponentId; 2]>,

crates/bevy_ecs/src/world/deferred_world.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -781,18 +781,18 @@ impl<'w> DeferredWorld<'w> {
781781
/// Triggers all event observers for [`ComponentId`] in target.
782782
///
783783
/// # Safety
784-
/// Caller must ensure observers listening for `event` can accept ZST pointers
784+
/// Caller must ensure observers listening for `event_key` can accept ZST pointers
785785
#[inline]
786786
pub(crate) unsafe fn trigger_observers(
787787
&mut self,
788-
event: EventKey,
788+
event_key: EventKey,
789789
target: Option<Entity>,
790790
components: impl Iterator<Item = ComponentId> + Clone,
791791
caller: MaybeLocation,
792792
) {
793793
Observers::invoke::<_>(
794794
self.reborrow(),
795-
event,
795+
event_key,
796796
target,
797797
target,
798798
components,
@@ -805,11 +805,11 @@ impl<'w> DeferredWorld<'w> {
805805
/// Triggers all event observers for [`ComponentId`] in target.
806806
///
807807
/// # Safety
808-
/// Caller must ensure `E` is accessible as the type represented by `event`
808+
/// Caller must ensure `E` is accessible as the type represented by `event_key`
809809
#[inline]
810810
pub(crate) unsafe fn trigger_observers_with_data<E, T>(
811811
&mut self,
812-
event: EventKey,
812+
event_key: EventKey,
813813
current_target: Option<Entity>,
814814
original_target: Option<Entity>,
815815
components: impl Iterator<Item = ComponentId> + Clone,
@@ -821,7 +821,7 @@ impl<'w> DeferredWorld<'w> {
821821
{
822822
Observers::invoke::<_>(
823823
self.reborrow(),
824-
event,
824+
event_key,
825825
current_target,
826826
original_target,
827827
components.clone(),
@@ -849,7 +849,7 @@ impl<'w> DeferredWorld<'w> {
849849
}
850850
Observers::invoke::<_>(
851851
self.reborrow(),
852-
event,
852+
event_key,
853853
Some(current_target),
854854
original_target,
855855
components.clone(),

0 commit comments

Comments
 (0)