Skip to content

Commit f135535

Browse files
Rename Command's "write" method to "apply" (#8814)
# Objective - Fixes #8811 . ## Solution - Rename "write" method to "apply" in Command trait definition. - Rename other implementations of command trait throughout bevy's code base. --- ## Changelog - Changed: `Command::write` has been changed to `Command::apply` - Changed: `EntityCommand::write` has been changed to `EntityCommand::apply` ## Migration Guide - `Command::write` implementations need to be changed to implement `Command::apply` instead. This is a mere name change, with no further actions needed. - `EntityCommand::write` implementations need to be changed to implement `EntityCommand::apply` instead. This is a mere name change, with no further actions needed. --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
1 parent ea887d8 commit f135535

File tree

8 files changed

+48
-44
lines changed

8 files changed

+48
-44
lines changed

benches/benches/bevy_ecs/world/commands.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ struct FakeCommandA;
125125
struct FakeCommandB(u64);
126126

127127
impl Command for FakeCommandA {
128-
fn write(self, world: &mut World) {
128+
fn apply(self, world: &mut World) {
129129
black_box(self);
130130
black_box(world);
131131
}
132132
}
133133

134134
impl Command for FakeCommandB {
135-
fn write(self, world: &mut World) {
135+
fn apply(self, world: &mut World) {
136136
black_box(self);
137137
black_box(world);
138138
}
@@ -169,7 +169,7 @@ pub fn fake_commands(criterion: &mut Criterion) {
169169
struct SizedCommand<T: Default + Send + Sync + 'static>(T);
170170

171171
impl<T: Default + Send + Sync + 'static> Command for SizedCommand<T> {
172-
fn write(self, world: &mut World) {
172+
fn apply(self, world: &mut World) {
173173
black_box(self);
174174
black_box(world);
175175
}

crates/bevy_ecs/src/system/commands/command_queue.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl CommandQueue {
5757
// SAFETY: According to the invariants of `CommandMeta.apply_command_and_get_size`,
5858
// `command` must point to a value of type `C`.
5959
let command: C = unsafe { command.read_unaligned() };
60-
command.write(world);
60+
command.apply(world);
6161
std::mem::size_of::<C>()
6262
},
6363
};
@@ -165,7 +165,7 @@ mod test {
165165
}
166166

167167
impl Command for DropCheck {
168-
fn write(self, _: &mut World) {}
168+
fn apply(self, _: &mut World) {}
169169
}
170170

171171
#[test]
@@ -191,7 +191,7 @@ mod test {
191191
struct SpawnCommand;
192192

193193
impl Command for SpawnCommand {
194-
fn write(self, world: &mut World) {
194+
fn apply(self, world: &mut World) {
195195
world.spawn_empty();
196196
}
197197
}
@@ -219,7 +219,7 @@ mod test {
219219
// some data added to it.
220220
struct PanicCommand(String);
221221
impl Command for PanicCommand {
222-
fn write(self, _: &mut World) {
222+
fn apply(self, _: &mut World) {
223223
panic!("command is panicking");
224224
}
225225
}
@@ -267,7 +267,7 @@ mod test {
267267

268268
struct CommandWithPadding(u8, u16);
269269
impl Command for CommandWithPadding {
270-
fn write(self, _: &mut World) {}
270+
fn apply(self, _: &mut World) {}
271271
}
272272

273273
#[cfg(miri)]

crates/bevy_ecs/src/system/commands/mod.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use super::{Deferred, Resource, SystemBuffer, SystemMeta};
3232
/// struct AddToCounter(u64);
3333
///
3434
/// impl Command for AddToCounter {
35-
/// fn write(self, world: &mut World) {
35+
/// fn apply(self, world: &mut World) {
3636
/// let mut counter = world.get_resource_or_insert_with(Counter::default);
3737
/// counter.0 += self.0;
3838
/// }
@@ -43,8 +43,12 @@ use super::{Deferred, Resource, SystemBuffer, SystemMeta};
4343
/// }
4444
/// ```
4545
pub trait Command: Send + 'static {
46-
/// Executes this command.
47-
fn write(self, world: &mut World);
46+
/// Applies this command, causing it to mutate the provided `world`.
47+
///
48+
/// This method is used to define what a command "does" when it is ultimately applied.
49+
/// Because this method takes `self`, you can store data or settings on the type that implements this trait.
50+
/// This data is set by the system or other source of the command, and then ultimately read in this method.
51+
fn apply(self, world: &mut World);
4852
}
4953

5054
/// A [`Command`] queue to perform impactful changes to the [`World`].
@@ -522,7 +526,7 @@ impl<'w, 's> Commands<'w, 's> {
522526
/// struct AddToCounter(u64);
523527
///
524528
/// impl Command for AddToCounter {
525-
/// fn write(self, world: &mut World) {
529+
/// fn apply(self, world: &mut World) {
526530
/// let mut counter = world.get_resource_or_insert_with(Counter::default);
527531
/// counter.0 += self.0;
528532
/// }
@@ -569,7 +573,7 @@ impl<'w, 's> Commands<'w, 's> {
569573
/// struct CountName;
570574
///
571575
/// impl EntityCommand for CountName {
572-
/// fn write(self, id: Entity, world: &mut World) {
576+
/// fn apply(self, id: Entity, world: &mut World) {
573577
/// // Get the current value of the counter, and increment it for next time.
574578
/// let mut counter = world.resource_mut::<Counter>();
575579
/// let i = counter.0;
@@ -605,7 +609,7 @@ impl<'w, 's> Commands<'w, 's> {
605609
/// ```
606610
pub trait EntityCommand: Send + 'static {
607611
/// Executes this command for the given [`Entity`].
608-
fn write(self, id: Entity, world: &mut World);
612+
fn apply(self, id: Entity, world: &mut World);
609613
/// Returns a [`Command`] which executes this [`EntityCommand`] for the given [`Entity`].
610614
fn with_entity(self, id: Entity) -> WithEntity<Self>
611615
where
@@ -623,8 +627,8 @@ pub struct WithEntity<C: EntityCommand> {
623627

624628
impl<C: EntityCommand> Command for WithEntity<C> {
625629
#[inline]
626-
fn write(self, world: &mut World) {
627-
self.cmd.write(self.id, world);
630+
fn apply(self, world: &mut World) {
631+
self.cmd.apply(self.id, world);
628632
}
629633
}
630634

@@ -829,7 +833,7 @@ impl<F> Command for F
829833
where
830834
F: FnOnce(&mut World) + Send + 'static,
831835
{
832-
fn write(self, world: &mut World) {
836+
fn apply(self, world: &mut World) {
833837
self(world);
834838
}
835839
}
@@ -838,7 +842,7 @@ impl<F> EntityCommand for F
838842
where
839843
F: FnOnce(Entity, &mut World) + Send + 'static,
840844
{
841-
fn write(self, id: Entity, world: &mut World) {
845+
fn apply(self, id: Entity, world: &mut World) {
842846
self(id, world);
843847
}
844848
}
@@ -854,7 +858,7 @@ impl<T> Command for Spawn<T>
854858
where
855859
T: Bundle,
856860
{
857-
fn write(self, world: &mut World) {
861+
fn apply(self, world: &mut World) {
858862
world.spawn(self.bundle);
859863
}
860864
}
@@ -876,7 +880,7 @@ where
876880
I: IntoIterator + Send + Sync + 'static,
877881
I::Item: Bundle,
878882
{
879-
fn write(self, world: &mut World) {
883+
fn apply(self, world: &mut World) {
880884
world.spawn_batch(self.bundles_iter);
881885
}
882886
}
@@ -901,7 +905,7 @@ where
901905
B: Bundle,
902906
I::IntoIter: Iterator<Item = (Entity, B)>,
903907
{
904-
fn write(self, world: &mut World) {
908+
fn apply(self, world: &mut World) {
905909
if let Err(invalid_entities) = world.insert_or_spawn_batch(self.bundles_iter) {
906910
error!(
907911
"Failed to 'insert or spawn' bundle of type {} into the following invalid entities: {:?}",
@@ -921,7 +925,7 @@ pub struct Despawn {
921925
}
922926

923927
impl Command for Despawn {
924-
fn write(self, world: &mut World) {
928+
fn apply(self, world: &mut World) {
925929
world.despawn(self.entity);
926930
}
927931
}
@@ -938,7 +942,7 @@ impl<T> Command for Insert<T>
938942
where
939943
T: Bundle + 'static,
940944
{
941-
fn write(self, world: &mut World) {
945+
fn apply(self, world: &mut World) {
942946
if let Some(mut entity) = world.get_entity_mut(self.entity) {
943947
entity.insert(self.bundle);
944948
} else {
@@ -961,7 +965,7 @@ impl<T> Command for Remove<T>
961965
where
962966
T: Bundle,
963967
{
964-
fn write(self, world: &mut World) {
968+
fn apply(self, world: &mut World) {
965969
if let Some(mut entity_mut) = world.get_entity_mut(self.entity) {
966970
entity_mut.remove::<T>();
967971
}
@@ -985,7 +989,7 @@ pub struct InitResource<R: Resource + FromWorld> {
985989
}
986990

987991
impl<R: Resource + FromWorld> Command for InitResource<R> {
988-
fn write(self, world: &mut World) {
992+
fn apply(self, world: &mut World) {
989993
world.init_resource::<R>();
990994
}
991995
}
@@ -1006,7 +1010,7 @@ pub struct InsertResource<R: Resource> {
10061010
}
10071011

10081012
impl<R: Resource> Command for InsertResource<R> {
1009-
fn write(self, world: &mut World) {
1013+
fn apply(self, world: &mut World) {
10101014
world.insert_resource(self.resource);
10111015
}
10121016
}
@@ -1017,7 +1021,7 @@ pub struct RemoveResource<R: Resource> {
10171021
}
10181022

10191023
impl<R: Resource> Command for RemoveResource<R> {
1020-
fn write(self, world: &mut World) {
1024+
fn apply(self, world: &mut World) {
10211025
world.remove_resource::<R>();
10221026
}
10231027
}
@@ -1037,7 +1041,7 @@ pub struct LogComponents {
10371041
}
10381042

10391043
impl Command for LogComponents {
1040-
fn write(self, world: &mut World) {
1044+
fn apply(self, world: &mut World) {
10411045
let debug_infos: Vec<_> = world
10421046
.inspect_entity(self.entity)
10431047
.into_iter()

crates/bevy_hierarchy/src/child_builder.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ pub struct AddChild {
169169
}
170170

171171
impl Command for AddChild {
172-
fn write(self, world: &mut World) {
172+
fn apply(self, world: &mut World) {
173173
world.entity_mut(self.parent).add_child(self.child);
174174
}
175175
}
@@ -183,7 +183,7 @@ pub struct InsertChildren {
183183
}
184184

185185
impl Command for InsertChildren {
186-
fn write(self, world: &mut World) {
186+
fn apply(self, world: &mut World) {
187187
world
188188
.entity_mut(self.parent)
189189
.insert_children(self.index, &self.children);
@@ -198,7 +198,7 @@ pub struct PushChildren {
198198
}
199199

200200
impl Command for PushChildren {
201-
fn write(self, world: &mut World) {
201+
fn apply(self, world: &mut World) {
202202
world.entity_mut(self.parent).push_children(&self.children);
203203
}
204204
}
@@ -210,7 +210,7 @@ pub struct RemoveChildren {
210210
}
211211

212212
impl Command for RemoveChildren {
213-
fn write(self, world: &mut World) {
213+
fn apply(self, world: &mut World) {
214214
remove_children(self.parent, &self.children, world);
215215
}
216216
}
@@ -222,7 +222,7 @@ pub struct ClearChildren {
222222
}
223223

224224
impl Command for ClearChildren {
225-
fn write(self, world: &mut World) {
225+
fn apply(self, world: &mut World) {
226226
clear_children(self.parent, world);
227227
}
228228
}
@@ -234,7 +234,7 @@ pub struct ReplaceChildren {
234234
}
235235

236236
impl Command for ReplaceChildren {
237-
fn write(self, world: &mut World) {
237+
fn apply(self, world: &mut World) {
238238
clear_children(self.parent, world);
239239
world.entity_mut(self.parent).push_children(&self.children);
240240
}
@@ -247,7 +247,7 @@ pub struct RemoveParent {
247247
}
248248

249249
impl Command for RemoveParent {
250-
fn write(self, world: &mut World) {
250+
fn apply(self, world: &mut World) {
251251
world.entity_mut(self.child).remove_parent();
252252
}
253253
}

crates/bevy_hierarchy/src/hierarchy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn despawn_children_recursive(world: &mut World, entity: Entity) {
5555
}
5656

5757
impl Command for DespawnRecursive {
58-
fn write(self, world: &mut World) {
58+
fn apply(self, world: &mut World) {
5959
#[cfg(feature = "trace")]
6060
let _span = bevy_utils::tracing::info_span!(
6161
"command",
@@ -68,7 +68,7 @@ impl Command for DespawnRecursive {
6868
}
6969

7070
impl Command for DespawnChildrenRecursive {
71-
fn write(self, world: &mut World) {
71+
fn apply(self, world: &mut World) {
7272
#[cfg(feature = "trace")]
7373
let _span = bevy_utils::tracing::info_span!(
7474
"command",

crates/bevy_scene/src/dynamic_scene.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ mod tests {
208208
parent: original_parent_entity,
209209
child: original_child_entity,
210210
}
211-
.write(&mut world);
211+
.apply(&mut world);
212212

213213
// We then write this relationship to a new scene, and then write that scene back to the
214214
// world to create another parent and child relationship
@@ -229,7 +229,7 @@ mod tests {
229229
parent: original_child_entity,
230230
child: from_scene_parent_entity,
231231
}
232-
.write(&mut world);
232+
.apply(&mut world);
233233

234234
// We then reload the scene to make sure that from_scene_parent_entity's parent component
235235
// isn't updated with the entity map, since this component isn't defined in the scene.

crates/bevy_scene/src/scene_spawner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ impl SceneSpawner {
284284
parent,
285285
child: entity,
286286
}
287-
.write(world);
287+
.apply(world);
288288
}
289289
}
290290
} else {

crates/bevy_transform/src/commands.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ pub struct AddChildInPlace {
2121
pub child: Entity,
2222
}
2323
impl Command for AddChildInPlace {
24-
fn write(self, world: &mut World) {
24+
fn apply(self, world: &mut World) {
2525
let hierarchy_command = AddChild {
2626
child: self.child,
2727
parent: self.parent,
2828
};
29-
hierarchy_command.write(world);
29+
hierarchy_command.apply(world);
3030
// FIXME: Replace this closure with a `try` block. See: https://github.com/rust-lang/rust/issues/31436.
3131
let mut update_transform = || {
3232
let parent = *world.get_entity(self.parent)?.get::<GlobalTransform>()?;
@@ -49,9 +49,9 @@ pub struct RemoveParentInPlace {
4949
pub child: Entity,
5050
}
5151
impl Command for RemoveParentInPlace {
52-
fn write(self, world: &mut World) {
52+
fn apply(self, world: &mut World) {
5353
let hierarchy_command = RemoveParent { child: self.child };
54-
hierarchy_command.write(world);
54+
hierarchy_command.apply(world);
5555
// FIXME: Replace this closure with a `try` block. See: https://github.com/rust-lang/rust/issues/31436.
5656
let mut update_transform = || {
5757
let child_global = *world.get_entity(self.child)?.get::<GlobalTransform>()?;

0 commit comments

Comments
 (0)