Skip to content

Commit 760a9e1

Browse files
committed
Remove InfallibleCommand trait
1 parent 5c5d22b commit 760a9e1

File tree

3 files changed

+41
-42
lines changed

3 files changed

+41
-42
lines changed

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ pub trait AddCommand {
99
fn add_command(&mut self, command: impl Command);
1010
}
1111

12+
/// Provides configuration mechanisms in case a command errors.
13+
/// You can specify a custom handler via [`FallibleCommandConfig::on_failure`] or
14+
/// use one of the provided implementations.
15+
///
16+
/// ## Note
17+
/// The default error handler logs the error (via [`error!`]), but does not panic.
1218
pub struct FallibleCommandConfig<'a, C, T>
1319
where
1420
C: FallibleCommand,
@@ -59,7 +65,7 @@ where
5965
}
6066

6167
macro_rules! impl_fallible_commands {
62-
($name:ident, $returnty:ty, $func:ident) => {
68+
($name:ident, $returnty:ty, $returnfunc:ident) => {
6369
impl<'a, C, T> $name<'a, C, T>
6470
where
6571
C: FallibleCommand,
@@ -82,7 +88,7 @@ macro_rules! impl_fallible_commands {
8288

8389
#[inline]
8490
#[allow(dead_code)]
85-
fn return_unit(&mut self) {}
91+
fn return_unit(&self) {}
8692
}
8793

8894
impl<'a, C, T> $name<'a, C, T>
@@ -91,6 +97,7 @@ macro_rules! impl_fallible_commands {
9197
C::Error: Debug,
9298
T: AddCommand,
9399
{
100+
/// If the command failed, run the provided `error_handler`.
94101
pub fn on_failure<'this, H>(&'this mut self, error_handler: H) -> $returnty
95102
where
96103
H: FnOnce(CommandError<'_, C>) + Send + Sync + 'static,
@@ -100,29 +107,34 @@ macro_rules! impl_fallible_commands {
100107
command,
101108
error_handler,
102109
});
103-
self.$func()
110+
self.$returnfunc()
104111
}
105112

113+
/// If the command failed, ignore the error and silently succeed.
106114
pub fn ignore<'this>(&'this mut self) -> $returnty {
107115
let command = self.command.take().unwrap();
108116
self.inner
109117
.add_command(IgnoreFallibleCommandWrapper(command));
110-
self.$func()
118+
self.$returnfunc()
111119
}
112120

113-
// this is the default
121+
/// If the command failed, log the error.
122+
///
123+
/// ## Note
124+
/// This is the default behavior if no error handler is specified.
114125
pub fn log_on_failure<'this>(&'this mut self) -> $returnty {
115126
let command = self.command.take().unwrap();
116127
self.inner
117128
.add_command(LoggingFallibleCommandWrapper(command));
118-
self.$func()
129+
self.$returnfunc()
119130
}
120131

132+
/// If the command failed, [`panic!`] with the error.
121133
pub fn panic_on_failure<'this>(&'this mut self) -> $returnty {
122134
let command = self.command.take().unwrap();
123135
self.inner
124136
.add_command(PanickingFallibleCommandWrapper(command));
125-
self.$func()
137+
self.$returnfunc()
126138
}
127139
}
128140

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@ use crate::world::World;
33
use bevy_utils::tracing::error;
44
use std::fmt::Debug;
55

6+
/// TODO:
67
pub struct CommandError<'a, C: FallibleCommand> {
78
pub error: C::Error,
89
pub world: &'a mut World,
910
}
1011

12+
/// Used for specifying and error for a command.
1113
pub trait FallibleCommand: Send + Sync + 'static {
1214
type Error;
1315

1416
fn try_write(self, world: &mut World) -> Result<(), Self::Error>;
1517
}
1618

17-
pub trait InfallibleCommand: Send + Sync + 'static {
18-
fn write(self, world: &mut World);
19-
}
20-
2119
pub(crate) struct IgnoreFallibleCommandWrapper<C: FallibleCommand>(pub(crate) C);
2220

2321
impl<C> Command for IgnoreFallibleCommandWrapper<C>
@@ -82,19 +80,3 @@ where
8280
}
8381
}
8482
}
85-
86-
pub(crate) struct InfallibleCommandWrapper<C>
87-
where
88-
C: InfallibleCommand,
89-
{
90-
pub(crate) command: C,
91-
}
92-
93-
impl<C> Command for InfallibleCommandWrapper<C>
94-
where
95-
C: InfallibleCommand,
96-
{
97-
fn write(self: Box<Self>, world: &mut World) {
98-
self.command.write(world);
99-
}
100-
}

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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ use crate::{
77
entity::{Entities, Entity},
88
world::World,
99
};
10-
use fallible::*;
1110
use std::{fmt::Debug, marker::PhantomData};
1211

13-
pub use config::*;
14-
pub use fallible::{FallibleCommand, InfallibleCommand};
12+
pub use config::{FallibleCommandConfig, FinalFallibleCommandConfig};
13+
pub use fallible::{CommandError, FallibleCommand};
1514

1615
/// A [`World`] mutation.
16+
/// If this could potentially fail, use [`FallibleCommand`].
1717
pub trait Command: Send + Sync + 'static {
1818
fn write(self: Box<Self>, world: &mut World);
1919
}
@@ -169,16 +169,12 @@ impl<'a> Commands<'a> {
169169
I: IntoIterator + Send + Sync + 'static,
170170
I::Item: Bundle,
171171
{
172-
self.queue.push(InfallibleCommandWrapper {
173-
command: SpawnBatch { bundles_iter },
174-
});
172+
self.queue.push(SpawnBatch { bundles_iter });
175173
}
176174

177175
/// See [`World::insert_resource`].
178176
pub fn insert_resource<T: Component>(&mut self, resource: T) {
179-
self.queue.push(InfallibleCommandWrapper {
180-
command: InsertResource { resource },
181-
})
177+
self.queue.push(InsertResource { resource })
182178
}
183179

184180
/// Queue a resource removal.
@@ -197,6 +193,15 @@ impl<'a> Commands<'a> {
197193
pub fn add<C: Command>(&mut self, command: C) {
198194
self.queue.push(command);
199195
}
196+
197+
/// Adds a fallible command to the command list.
198+
pub fn add_fallible<C>(&mut self, command: C) -> FinalFallibleCommandConfig<'_, C, Self>
199+
where
200+
C: FallibleCommand,
201+
C::Error: Debug,
202+
{
203+
FinalFallibleCommandConfig::new(command, self)
204+
}
200205
}
201206

202207
impl<'a> config::AddCommand for Commands<'a> {
@@ -332,11 +337,11 @@ pub struct Spawn<T> {
332337
pub bundle: T,
333338
}
334339

335-
impl<T> InfallibleCommand for Spawn<T>
340+
impl<T> Command for Spawn<T>
336341
where
337342
T: Bundle,
338343
{
339-
fn write(self, world: &mut World) {
344+
fn write(self: Box<Self>, world: &mut World) {
340345
world.spawn().insert_bundle(self.bundle);
341346
}
342347
}
@@ -349,12 +354,12 @@ where
349354
pub bundles_iter: I,
350355
}
351356

352-
impl<I> InfallibleCommand for SpawnBatch<I>
357+
impl<I> Command for SpawnBatch<I>
353358
where
354359
I: IntoIterator + Send + Sync + 'static,
355360
I::Item: Bundle,
356361
{
357-
fn write(self, world: &mut World) {
362+
fn write(self: Box<Self>, world: &mut World) {
358363
world.spawn_batch(self.bundles_iter);
359364
}
360365
}
@@ -545,8 +550,8 @@ pub struct InsertResource<T: Component> {
545550
pub resource: T,
546551
}
547552

548-
impl<T: Component> InfallibleCommand for InsertResource<T> {
549-
fn write(self, world: &mut World) {
553+
impl<T: Component> Command for InsertResource<T> {
554+
fn write(self: Box<Self>, world: &mut World) {
550555
world.insert_resource(self.resource);
551556
}
552557
}

0 commit comments

Comments
 (0)