From 85196b34c5b9a1aebcd444e9bc2c90e18e78474e Mon Sep 17 00:00:00 2001 From: Aevyrie Date: Sun, 17 Jul 2022 09:00:11 -0700 Subject: [PATCH 1/9] add event sending helpers to `World` --- crates/bevy_ecs/src/world/mod.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index fb36921bafe7d..11c926ebd26d3 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1156,6 +1156,18 @@ impl World { result } + /// "Sends" an `event` by writing it to the current event buffer. [`EventReader`]s can then read + /// the event. + pub fn event_send(&mut self, event: E) { + self.resource_mut::>().send(event); + } + + /// Sends the default value of the event. Useful when the event is an empty struct. + pub fn event_send_default(&mut self) { + self.resource_mut::>() + .send_default(); + } + /// # Safety /// `component_id` must be assigned to a component of type `R` #[inline] From db0369789a475760366238408c6bfb226781a4be Mon Sep 17 00:00:00 2001 From: Aevyrie Date: Sun, 17 Jul 2022 09:13:30 -0700 Subject: [PATCH 2/9] make doc comments concise --- crates/bevy_ecs/src/world/mod.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 11c926ebd26d3..b28e6ae49d98a 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1156,13 +1156,14 @@ impl World { result } - /// "Sends" an `event` by writing it to the current event buffer. [`EventReader`]s can then read - /// the event. + /// "Sends" an `event`. + #[inline] pub fn event_send(&mut self, event: E) { self.resource_mut::>().send(event); } - /// Sends the default value of the event. Useful when the event is an empty struct. + /// Sends the default value of the event. + #[inline] pub fn event_send_default(&mut self) { self.resource_mut::>() .send_default(); From 729d3d2977788426b09d663e8c0d83cac14d8f1d Mon Sep 17 00:00:00 2001 From: Aevyrie Date: Sun, 17 Jul 2022 11:25:20 -0700 Subject: [PATCH 3/9] Update crates/bevy_ecs/src/world/mod.rs Co-authored-by: Federico Rinaldi --- crates/bevy_ecs/src/world/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index b28e6ae49d98a..a100065634c8f 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1156,7 +1156,7 @@ impl World { result } - /// "Sends" an `event`. + /// "Sends" an [event](crate::event). #[inline] pub fn event_send(&mut self, event: E) { self.resource_mut::>().send(event); From 62512d50a4a16b6488d0804304584275a60f5f37 Mon Sep 17 00:00:00 2001 From: Aevyrie Date: Sun, 17 Jul 2022 11:25:34 -0700 Subject: [PATCH 4/9] Update crates/bevy_ecs/src/world/mod.rs Co-authored-by: Federico Rinaldi --- crates/bevy_ecs/src/world/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index a100065634c8f..65fc71d135b3e 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1162,7 +1162,7 @@ impl World { self.resource_mut::>().send(event); } - /// Sends the default value of the event. + /// Sends the default value of the [event](crate::event). #[inline] pub fn event_send_default(&mut self) { self.resource_mut::>() From e4b489c2372624ed81856c3f99b6579b7ca82745 Mon Sep 17 00:00:00 2001 From: Aevyrie Date: Sun, 17 Jul 2022 12:07:10 -0700 Subject: [PATCH 5/9] Improve error message and deduplicate --- crates/bevy_ecs/src/world/mod.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 65fc71d135b3e..63e467efcecd1 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1156,17 +1156,22 @@ impl World { result } - /// "Sends" an [event](crate::event). + /// Sends an [Event](crate::event::Event). #[inline] - pub fn event_send(&mut self, event: E) { - self.resource_mut::>().send(event); + pub fn send_event(&mut self, event: E) { + match self.get_resource_mut::>() { + Some(mut events) => events.send(event), + None => bevy_utils::tracing::error!( + "Unable to send event `{}`\n\tEvent must be added to the app with `add_event()`\n\thttps://docs.rs/bevy/*/bevy/app/struct.App.html#method.add_event ", + std::any::type_name::() + ), + } } - /// Sends the default value of the [event](crate::event). + /// Sends the default value of the [Event](crate::event::Event) of type `E`. #[inline] - pub fn event_send_default(&mut self) { - self.resource_mut::>() - .send_default(); + pub fn send_default_event(&mut self) { + self.send_event(E::default()); } /// # Safety From 1371e2cb7fd9036606b370399a20c9e9fa4cc403 Mon Sep 17 00:00:00 2001 From: Aevyrie Date: Mon, 18 Jul 2022 12:12:31 -0700 Subject: [PATCH 6/9] Update crates/bevy_ecs/src/world/mod.rs Co-authored-by: Federico Rinaldi --- crates/bevy_ecs/src/world/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 63e467efcecd1..55dddbe671f8f 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1168,7 +1168,7 @@ impl World { } } - /// Sends the default value of the [Event](crate::event::Event) of type `E`. + /// Sends the default value of the [`Event`](crate::event::Event) of type `E`. #[inline] pub fn send_default_event(&mut self) { self.send_event(E::default()); From 973bbdf594d8f3c3b851042861d5d36da355791f Mon Sep 17 00:00:00 2001 From: Aevyrie Date: Mon, 18 Jul 2022 12:12:37 -0700 Subject: [PATCH 7/9] Update crates/bevy_ecs/src/world/mod.rs Co-authored-by: Federico Rinaldi --- crates/bevy_ecs/src/world/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 55dddbe671f8f..91b66fbc7f967 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1156,7 +1156,7 @@ impl World { result } - /// Sends an [Event](crate::event::Event). + /// Sends an [`Event`](crate::event::Event). #[inline] pub fn send_event(&mut self, event: E) { match self.get_resource_mut::>() { From 3e6ae215eaf5664fa0faee1c40441e84abfc8cfb Mon Sep 17 00:00:00 2001 From: Aevyrie Date: Mon, 18 Jul 2022 18:36:46 -0700 Subject: [PATCH 8/9] add send_batch --- crates/bevy_ecs/src/world/mod.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 91b66fbc7f967..0f453dd3c1397 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1159,8 +1159,20 @@ impl World { /// Sends an [`Event`](crate::event::Event). #[inline] pub fn send_event(&mut self, event: E) { + self.send_batch(std::iter::once(event)); + } + + /// Sends the default value of the [`Event`](crate::event::Event) of type `E`. + #[inline] + pub fn send_default_event(&mut self) { + self.send_batch(std::iter::once(E::default())); + } + + /// Sends a batch of [`Event`](crate::event::Event)s from an iterator. + #[inline] + pub fn send_batch(&mut self, events: impl Iterator) { match self.get_resource_mut::>() { - Some(mut events) => events.send(event), + Some(mut events_resource) => events_resource.extend(events), None => bevy_utils::tracing::error!( "Unable to send event `{}`\n\tEvent must be added to the app with `add_event()`\n\thttps://docs.rs/bevy/*/bevy/app/struct.App.html#method.add_event ", std::any::type_name::() @@ -1168,12 +1180,6 @@ impl World { } } - /// Sends the default value of the [`Event`](crate::event::Event) of type `E`. - #[inline] - pub fn send_default_event(&mut self) { - self.send_event(E::default()); - } - /// # Safety /// `component_id` must be assigned to a component of type `R` #[inline] From 3eaf8d80463c5b000a41dc58e8ecd4c8ac078335 Mon Sep 17 00:00:00 2001 From: Aevyrie Date: Tue, 19 Jul 2022 13:24:27 -0700 Subject: [PATCH 9/9] Rename `send_batch` to `send_event_batch` --- crates/bevy_ecs/src/world/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 0f453dd3c1397..2bbec2aaa9e85 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1159,18 +1159,18 @@ impl World { /// Sends an [`Event`](crate::event::Event). #[inline] pub fn send_event(&mut self, event: E) { - self.send_batch(std::iter::once(event)); + self.send_event_batch(std::iter::once(event)); } /// Sends the default value of the [`Event`](crate::event::Event) of type `E`. #[inline] pub fn send_default_event(&mut self) { - self.send_batch(std::iter::once(E::default())); + self.send_event_batch(std::iter::once(E::default())); } /// Sends a batch of [`Event`](crate::event::Event)s from an iterator. #[inline] - pub fn send_batch(&mut self, events: impl Iterator) { + pub fn send_event_batch(&mut self, events: impl Iterator) { match self.get_resource_mut::>() { Some(mut events_resource) => events_resource.extend(events), None => bevy_utils::tracing::error!(