Skip to content

Commit 3aed85a

Browse files
AlephCubedshwwwa
andauthored
Rename send_event and similar methods to write_event (#20017)
Fixes: #18963 Follows up on: #17977 Adopts: #18966 In 0.16, `EventWriter::send` was renamed to `EventWriter::write`, but many methods were missed (sorry about that). This completes that refactor by renaming all `send` methods and internals. | Old | New | |-------------------------------------|--------------------------------------| | `World::send_event` | `World::write_event` | | `World::send_event_default` | `World::write_event_default` | | `World::send_event_batch` | `World::write_event_batch` | | `DeferredWorld::send_event` | `DeferredWorld::write_event` | | `DeferredWorld::send_event_default` | `DeferredWorld::write_event_default` | | `DeferredWorld::send_event_batch` | `DeferredWorld::write_event_batch` | | `Commands::send_event` | `Commmands::write_event` | | `Events::send` | `Events::write` | | `Events::send_default` | `Events::write_default` | | `Events::send_batch` | `Events::write_batch` | | `RemovedComponentEvents::send` | `RemovedComponentEvents::write` | | `command::send_event` | `commmand::write_event` | | `SendBatchIds` | `WriteBatchIds` | --------- Co-authored-by: shwwwa <shwwwa.dev@gmail.com>
1 parent fb5d8fd commit 3aed85a

File tree

34 files changed

+364
-210
lines changed

34 files changed

+364
-210
lines changed

benches/benches/bevy_ecs/events/iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl<const SIZE: usize> Benchmark<SIZE> {
1010
let mut events = Events::default();
1111

1212
for _ in 0..count {
13-
events.send(BenchEvent([0u8; SIZE]));
13+
events.write(BenchEvent([0u8; SIZE]));
1414
}
1515

1616
Self(events)

benches/benches/bevy_ecs/events/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod iter;
2-
mod send;
2+
mod write;
33

44
use criterion::{criterion_group, Criterion};
55

@@ -11,19 +11,19 @@ fn send(c: &mut Criterion) {
1111
group.measurement_time(core::time::Duration::from_secs(4));
1212
for count in [100, 1_000, 10_000] {
1313
group.bench_function(format!("size_4_events_{count}"), |b| {
14-
let mut bench = send::Benchmark::<4>::new(count);
14+
let mut bench = write::Benchmark::<4>::new(count);
1515
b.iter(move || bench.run());
1616
});
1717
}
1818
for count in [100, 1_000, 10_000] {
1919
group.bench_function(format!("size_16_events_{count}"), |b| {
20-
let mut bench = send::Benchmark::<16>::new(count);
20+
let mut bench = write::Benchmark::<16>::new(count);
2121
b.iter(move || bench.run());
2222
});
2323
}
2424
for count in [100, 1_000, 10_000] {
2525
group.bench_function(format!("size_512_events_{count}"), |b| {
26-
let mut bench = send::Benchmark::<512>::new(count);
26+
let mut bench = write::Benchmark::<512>::new(count);
2727
b.iter(move || bench.run());
2828
});
2929
}

benches/benches/bevy_ecs/events/send.rs renamed to benches/benches/bevy_ecs/events/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl<const SIZE: usize> Benchmark<SIZE> {
2121
// Force both internal buffers to be allocated.
2222
for _ in 0..2 {
2323
for _ in 0..count {
24-
events.send(BenchEvent([0u8; SIZE]));
24+
events.write(BenchEvent([0u8; SIZE]));
2525
}
2626
events.update();
2727
}
@@ -32,7 +32,7 @@ impl<const SIZE: usize> Benchmark<SIZE> {
3232
pub fn run(&mut self) {
3333
for _ in 0..self.count {
3434
self.events
35-
.send(core::hint::black_box(BenchEvent([0u8; SIZE])));
35+
.write(core::hint::black_box(BenchEvent([0u8; SIZE])));
3636
}
3737
self.events.update();
3838
}

crates/bevy_app/src/app.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,16 +1864,16 @@ mod tests {
18641864
app.update();
18651865

18661866
// Sending one event
1867-
app.world_mut().send_event(TestEvent);
1867+
app.world_mut().write_event(TestEvent);
18681868

18691869
let test_events = app.world().resource::<Events<TestEvent>>();
18701870
assert_eq!(test_events.len(), 1);
18711871
assert_eq!(test_events.iter_current_update_events().count(), 1);
18721872
app.update();
18731873

18741874
// Sending two events on the next frame
1875-
app.world_mut().send_event(TestEvent);
1876-
app.world_mut().send_event(TestEvent);
1875+
app.world_mut().write_event(TestEvent);
1876+
app.world_mut().write_event(TestEvent);
18771877

18781878
let test_events = app.world().resource::<Events<TestEvent>>();
18791879
assert_eq!(test_events.len(), 3); // Events are double-buffered, so we see 1 + 2 = 3

crates/bevy_asset/src/server/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl AssetServer {
167167
fn sender<A: Asset>(world: &mut World, id: UntypedAssetId) {
168168
world
169169
.resource_mut::<Events<AssetEvent<A>>>()
170-
.send(AssetEvent::LoadedWithDependencies { id: id.typed() });
170+
.write(AssetEvent::LoadedWithDependencies { id: id.typed() });
171171
}
172172
fn failed_sender<A: Asset>(
173173
world: &mut World,
@@ -177,7 +177,7 @@ impl AssetServer {
177177
) {
178178
world
179179
.resource_mut::<Events<AssetLoadFailedEvent<A>>>()
180-
.send(AssetLoadFailedEvent {
180+
.write(AssetLoadFailedEvent {
181181
id: id.typed(),
182182
path,
183183
error,
@@ -1685,7 +1685,7 @@ pub fn handle_internal_asset_events(world: &mut World) {
16851685
}
16861686

16871687
if !untyped_failures.is_empty() {
1688-
world.send_event_batch(untyped_failures);
1688+
world.write_event_batch(untyped_failures);
16891689
}
16901690

16911691
fn queue_ancestors(

crates/bevy_dev_tools/src/ci_testing/systems.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(crate) fn send_events(world: &mut World, mut current_frame: Local<u32>) {
1818
debug!("Handling event: {:?}", event);
1919
match event {
2020
CiTestingEvent::AppExit => {
21-
world.send_event(AppExit::Success);
21+
world.write_event(AppExit::Success);
2222
info!("Exiting after {} frames. Test successful!", *current_frame);
2323
}
2424
CiTestingEvent::ScreenshotAndExit => {
@@ -53,7 +53,7 @@ pub(crate) fn send_events(world: &mut World, mut current_frame: Local<u32>) {
5353
}
5454
// Custom events are forwarded to the world.
5555
CiTestingEvent::Custom(event_string) => {
56-
world.send_event(CiTestingCustomEvent(event_string));
56+
world.write_event(CiTestingCustomEvent(event_string));
5757
}
5858
}
5959
}

crates/bevy_ecs/src/bundle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1584,7 +1584,7 @@ impl<'w> BundleRemover<'w> {
15841584
// Handle sparse set removes
15851585
for component_id in self.bundle_info.as_ref().iter_explicit_components() {
15861586
if self.old_archetype.as_ref().contains(component_id) {
1587-
world.removed_components.send(component_id, entity);
1587+
world.removed_components.write(component_id, entity);
15881588

15891589
// Make sure to drop components stored in sparse sets.
15901590
// Dense components are dropped later in `move_to_and_drop_missing_unchecked`.

crates/bevy_ecs/src/event/collections.rs

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ use {
5454
/// // run this once per update/frame
5555
/// events.update();
5656
///
57-
/// // somewhere else: send an event
58-
/// events.send(MyEvent { value: 1 });
57+
/// // somewhere else: write an event
58+
/// events.write(MyEvent { value: 1 });
5959
///
6060
/// // somewhere else: read the events
6161
/// for event in cursor.read(&events) {
@@ -118,22 +118,22 @@ impl<E: BufferedEvent> Events<E> {
118118
self.events_a.start_event_count
119119
}
120120

121-
/// "Sends" an `event` by writing it to the current event buffer.
121+
/// Writes an `event` to the current event buffer.
122122
/// [`EventReader`](super::EventReader)s can then read the event.
123-
/// This method returns the [ID](`EventId`) of the sent `event`.
123+
/// This method returns the [ID](`EventId`) of the written `event`.
124124
#[track_caller]
125-
pub fn send(&mut self, event: E) -> EventId<E> {
126-
self.send_with_caller(event, MaybeLocation::caller())
125+
pub fn write(&mut self, event: E) -> EventId<E> {
126+
self.write_with_caller(event, MaybeLocation::caller())
127127
}
128128

129-
pub(crate) fn send_with_caller(&mut self, event: E, caller: MaybeLocation) -> EventId<E> {
129+
pub(crate) fn write_with_caller(&mut self, event: E, caller: MaybeLocation) -> EventId<E> {
130130
let event_id = EventId {
131131
id: self.event_count,
132132
caller,
133133
_marker: PhantomData,
134134
};
135135
#[cfg(feature = "detailed_trace")]
136-
tracing::trace!("Events::send() -> id: {}", event_id);
136+
tracing::trace!("Events::write() -> id: {}", event_id);
137137

138138
let event_instance = EventInstance { event_id, event };
139139

@@ -143,30 +143,59 @@ impl<E: BufferedEvent> Events<E> {
143143
event_id
144144
}
145145

146-
/// Sends a list of `events` all at once, which can later be read by [`EventReader`](super::EventReader)s.
147-
/// This is more efficient than sending each event individually.
148-
/// This method returns the [IDs](`EventId`) of the sent `events`.
146+
/// Writes a list of `events` all at once, which can later be read by [`EventReader`](super::EventReader)s.
147+
/// This is more efficient than writing each event individually.
148+
/// This method returns the [IDs](`EventId`) of the written `events`.
149149
#[track_caller]
150-
pub fn send_batch(&mut self, events: impl IntoIterator<Item = E>) -> SendBatchIds<E> {
150+
pub fn write_batch(&mut self, events: impl IntoIterator<Item = E>) -> WriteBatchIds<E> {
151151
let last_count = self.event_count;
152152

153153
self.extend(events);
154154

155-
SendBatchIds {
155+
WriteBatchIds {
156156
last_count,
157157
event_count: self.event_count,
158158
_marker: PhantomData,
159159
}
160160
}
161161

162+
/// Writes the default value of the event. Useful when the event is an empty struct.
163+
/// This method returns the [ID](`EventId`) of the written `event`.
164+
#[track_caller]
165+
pub fn write_default(&mut self) -> EventId<E>
166+
where
167+
E: Default,
168+
{
169+
self.write(Default::default())
170+
}
171+
172+
/// "Sends" an `event` by writing it to the current event buffer.
173+
/// [`EventReader`](super::EventReader)s can then read the event.
174+
/// This method returns the [ID](`EventId`) of the sent `event`.
175+
#[deprecated(since = "0.17.0", note = "Use `Events<E>::write` instead.")]
176+
#[track_caller]
177+
pub fn send(&mut self, event: E) -> EventId<E> {
178+
self.write(event)
179+
}
180+
181+
/// Sends a list of `events` all at once, which can later be read by [`EventReader`](super::EventReader)s.
182+
/// This is more efficient than sending each event individually.
183+
/// This method returns the [IDs](`EventId`) of the sent `events`.
184+
#[deprecated(since = "0.17.0", note = "Use `Events<E>::write_batch` instead.")]
185+
#[track_caller]
186+
pub fn send_batch(&mut self, events: impl IntoIterator<Item = E>) -> WriteBatchIds<E> {
187+
self.write_batch(events)
188+
}
189+
162190
/// Sends the default value of the event. Useful when the event is an empty struct.
163191
/// This method returns the [ID](`EventId`) of the sent `event`.
192+
#[deprecated(since = "0.17.0", note = "Use `Events<E>::write_default` instead.")]
164193
#[track_caller]
165194
pub fn send_default(&mut self) -> EventId<E>
166195
where
167196
E: Default,
168197
{
169-
self.send(Default::default())
198+
self.write_default()
170199
}
171200

172201
/// Gets a new [`EventCursor`]. This will include all events already in the event buffers.
@@ -351,14 +380,18 @@ impl<E: BufferedEvent> DerefMut for EventSequence<E> {
351380
}
352381
}
353382

354-
/// [`Iterator`] over sent [`EventIds`](`EventId`) from a batch.
355-
pub struct SendBatchIds<E> {
383+
/// [`Iterator`] over written [`EventIds`](`EventId`) from a batch.
384+
pub struct WriteBatchIds<E> {
356385
last_count: usize,
357386
event_count: usize,
358387
_marker: PhantomData<E>,
359388
}
360389

361-
impl<E: BufferedEvent> Iterator for SendBatchIds<E> {
390+
/// [`Iterator`] over sent [`EventIds`](`EventId`) from a batch.
391+
#[deprecated(since = "0.17.0", note = "Use `WriteBatchIds` instead.")]
392+
pub type SendBatchIds<E> = WriteBatchIds<E>;
393+
394+
impl<E: BufferedEvent> Iterator for WriteBatchIds<E> {
362395
type Item = EventId<E>;
363396

364397
fn next(&mut self) -> Option<Self::Item> {
@@ -378,7 +411,7 @@ impl<E: BufferedEvent> Iterator for SendBatchIds<E> {
378411
}
379412
}
380413

381-
impl<E: BufferedEvent> ExactSizeIterator for SendBatchIds<E> {
414+
impl<E: BufferedEvent> ExactSizeIterator for WriteBatchIds<E> {
382415
fn len(&self) -> usize {
383416
self.event_count.saturating_sub(self.last_count)
384417
}
@@ -400,22 +433,22 @@ mod tests {
400433
assert_eq!(test_events.iter_current_update_events().count(), 0);
401434
test_events.update();
402435

403-
// Sending one event
404-
test_events.send(TestEvent);
436+
// Writing one event
437+
test_events.write(TestEvent);
405438

406439
assert_eq!(test_events.len(), 1);
407440
assert_eq!(test_events.iter_current_update_events().count(), 1);
408441
test_events.update();
409442

410-
// Sending two events on the next frame
411-
test_events.send(TestEvent);
412-
test_events.send(TestEvent);
443+
// Writing two events on the next frame
444+
test_events.write(TestEvent);
445+
test_events.write(TestEvent);
413446

414447
assert_eq!(test_events.len(), 3); // Events are double-buffered, so we see 1 + 2 = 3
415448
assert_eq!(test_events.iter_current_update_events().count(), 2);
416449
test_events.update();
417450

418-
// Sending zero events
451+
// Writing zero events
419452
assert_eq!(test_events.len(), 2); // Events are double-buffered, so we see 2 + 0 = 2
420453
assert_eq!(test_events.iter_current_update_events().count(), 0);
421454
}

crates/bevy_ecs/src/event/event_cursor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use core::marker::PhantomData;
4141
/// }
4242
///
4343
/// for event in events_to_resend {
44-
/// events.send(MyEvent);
44+
/// events.write(MyEvent);
4545
/// }
4646
/// }
4747
///

0 commit comments

Comments
 (0)