Skip to content

Commit 283654c

Browse files
authored
Small Commands error handling cleanup (#17904)
- Remove references to the short-lived `CommandError` type. - Add a sentence to the explanation of error handlers. - Clean up spacing/linebreaks. - Use `where` notation for command-related trait `impl`s to make the big ones easier to parse.
1 parent 726d8ac commit 283654c

File tree

3 files changed

+57
-30
lines changed

3 files changed

+57
-30
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ pub trait HandleError<Out = ()> {
7979
}
8080
}
8181

82-
impl<C: Command<Result<T, E>>, T, E: Into<Error>> HandleError<Result<T, E>> for C {
82+
impl<C, T, E> HandleError<Result<T, E>> for C
83+
where
84+
C: Command<Result<T, E>>,
85+
E: Into<Error>,
86+
{
8387
fn handle_error_with(self, error_handler: fn(&mut World, Error)) -> impl Command {
8488
move |world: &mut World| match self.apply(world) {
8589
Ok(_) => {}
@@ -88,7 +92,10 @@ impl<C: Command<Result<T, E>>, T, E: Into<Error>> HandleError<Result<T, E>> for
8892
}
8993
}
9094

91-
impl<C: Command> HandleError for C {
95+
impl<C> HandleError for C
96+
where
97+
C: Command,
98+
{
9299
#[inline]
93100
fn handle_error_with(self, _error_handler: fn(&mut World, Error)) -> impl Command {
94101
self

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! [`EntityCommands`](crate::system::EntityCommands).
66
77
use alloc::vec::Vec;
8+
use core::fmt;
89
use log::info;
910

1011
use crate::{
@@ -79,8 +80,7 @@ use bevy_ptr::OwningPtr;
7980
/// }
8081
/// ```
8182
pub trait EntityCommand<Out = ()>: Send + 'static {
82-
/// Executes this command for the given [`Entity`] and
83-
/// returns a [`Result`] for error handling.
83+
/// Executes this command for the given [`Entity`].
8484
fn apply(self, entity: EntityWorldMut) -> Out;
8585
}
8686
/// Passes in a specific entity to an [`EntityCommand`], resulting in a [`Command`] that
@@ -96,7 +96,10 @@ pub trait CommandWithEntity<Out> {
9696
fn with_entity(self, entity: Entity) -> impl Command<Out> + HandleError<Out>;
9797
}
9898

99-
impl<C: EntityCommand> CommandWithEntity<Result<(), EntityMutableFetchError>> for C {
99+
impl<C> CommandWithEntity<Result<(), EntityMutableFetchError>> for C
100+
where
101+
C: EntityCommand,
102+
{
100103
fn with_entity(
101104
self,
102105
entity: Entity,
@@ -110,11 +113,10 @@ impl<C: EntityCommand> CommandWithEntity<Result<(), EntityMutableFetchError>> fo
110113
}
111114
}
112115

113-
impl<
114-
C: EntityCommand<Result<T, Err>>,
115-
T,
116-
Err: core::fmt::Debug + core::fmt::Display + Send + Sync + 'static,
117-
> CommandWithEntity<Result<T, EntityCommandError<Err>>> for C
116+
impl<C, T, Err> CommandWithEntity<Result<T, EntityCommandError<Err>>> for C
117+
where
118+
C: EntityCommand<Result<T, Err>>,
119+
Err: fmt::Debug + fmt::Display + Send + Sync + 'static,
118120
{
119121
fn with_entity(
120122
self,
@@ -245,8 +247,9 @@ pub fn retain<T: Bundle>() -> impl EntityCommand {
245247
///
246248
/// # Note
247249
///
248-
/// This will also despawn any [`Children`](crate::hierarchy::Children) entities, and any other [`RelationshipTarget`](crate::relationship::RelationshipTarget) that is configured
249-
/// to despawn descendants. This results in "recursive despawn" behavior.
250+
/// This will also despawn any [`Children`](crate::hierarchy::Children) entities,
251+
/// and any other [`RelationshipTarget`](crate::relationship::RelationshipTarget) that is configured to despawn descendants.
252+
/// This results in "recursive despawn" behavior.
250253
#[track_caller]
251254
pub fn despawn() -> impl EntityCommand {
252255
let caller = MaybeLocation::caller();

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

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,19 @@ use crate::{
8989
///
9090
/// # Error handling
9191
///
92-
/// Commands can return a [`Result`](crate::result::Result), which can be passed to
93-
/// an error handler. Error handlers are functions/closures of the form
94-
/// `fn(&mut World, CommandError)`.
92+
/// A [`Command`] can return a [`Result`](crate::result::Result),
93+
/// which will be passed to an error handler if the `Result` is an error.
9594
///
96-
/// The default error handler panics. It can be configured by enabling the `configurable_error_handler`
97-
/// cargo feature, then setting the `GLOBAL_ERROR_HANDLER`.
95+
/// Error handlers are functions/closures of the form `fn(&mut World, Error)`.
96+
/// They are granted exclusive access to the [`World`], which enables them to
97+
/// respond to the error in whatever way is necessary.
9898
///
99-
/// Alternatively, you can customize the error handler for a specific command by calling [`Commands::queue_handled`].
99+
/// The [default error handler](error_handler::default) panics.
100+
/// It can be configured by enabling the `configurable_error_handler` cargo feature,
101+
/// then setting the `GLOBAL_ERROR_HANDLER`.
102+
///
103+
/// Alternatively, you can customize the error handler for a specific command
104+
/// by calling [`Commands::queue_handled`].
100105
///
101106
/// The [`error_handler`] module provides some simple error handlers for convenience.
102107
///
@@ -546,7 +551,8 @@ impl<'w, 's> Commands<'w, 's> {
546551

547552
/// Pushes a generic [`Command`] to the command queue.
548553
///
549-
/// If the [`Command`] returns a [`Result`], it will be handled using the [default error handler](error_handler::default).
554+
/// If the [`Command`] returns a [`Result`],
555+
/// it will be handled using the [default error handler](error_handler::default).
550556
///
551557
/// To use a custom error handler, see [`Commands::queue_handled`].
552558
///
@@ -589,8 +595,11 @@ impl<'w, 's> Commands<'w, 's> {
589595
pub fn queue<C: Command<T> + HandleError<T>, T>(&mut self, command: C) {
590596
self.queue_internal(command.handle_error());
591597
}
592-
/// Pushes a generic [`Command`] to the command queue. If the command returns a [`Result`] the given
593-
/// `error_handler` will be used to handle error cases.
598+
599+
/// Pushes a generic [`Command`] to the command queue.
600+
///
601+
/// If the [`Command`] returns a [`Result`],
602+
/// the given `error_handler` will be used to handle error cases.
594603
///
595604
/// To implicitly use the default error handler, see [`Commands::queue`].
596605
///
@@ -1137,7 +1146,7 @@ impl<'w, 's> Commands<'w, 's> {
11371146
/// Most [`Commands`] (and thereby [`EntityCommands`]) are deferred: when you call the command,
11381147
/// if it requires mutable access to the [`World`] (that is, if it removes, adds, or changes something),
11391148
/// it's not executed immediately. Instead, the command is added to a "command queue."
1140-
/// The command queue is applied between [`Schedules`](bevy_ecs::schedule::Schedule), one by one,
1149+
/// The command queue is applied between [`Schedules`](crate::schedule::Schedule), one by one,
11411150
/// so that each command can have exclusive access to the World.
11421151
///
11431152
/// # Fallible
@@ -1148,14 +1157,19 @@ impl<'w, 's> Commands<'w, 's> {
11481157
///
11491158
/// # Error handling
11501159
///
1151-
/// [`EntityCommands`] can return a [`Result`](crate::result::Result), which can be passed to
1152-
/// an error handler. Error handlers are functions/closures of the form
1153-
/// `fn(&mut World, CommandError)`.
1160+
/// An [`EntityCommand`] can return a [`Result`](crate::result::Result),
1161+
/// which will be passed to an error handler if the `Result` is an error.
1162+
///
1163+
/// Error handlers are functions/closures of the form `fn(&mut World, Error)`.
1164+
/// They are granted exclusive access to the [`World`], which enables them to
1165+
/// respond to the error in whatever way is necessary.
11541166
///
1155-
/// The default error handler panics. It can be configured by enabling the `configurable_error_handler`
1156-
/// cargo feature, then setting the `GLOBAL_ERROR_HANDLER`.
1167+
/// The [default error handler](error_handler::default) panics.
1168+
/// It can be configured by enabling the `configurable_error_handler` cargo feature,
1169+
/// then setting the `GLOBAL_ERROR_HANDLER`.
11571170
///
1158-
/// Alternatively, you can customize the error handler for a specific command by calling [`EntityCommands::queue_handled`].
1171+
/// Alternatively, you can customize the error handler for a specific command
1172+
/// by calling [`EntityCommands::queue_handled`].
11591173
///
11601174
/// The [`error_handler`] module provides some simple error handlers for convenience.
11611175
pub struct EntityCommands<'a> {
@@ -1754,7 +1768,8 @@ impl<'a> EntityCommands<'a> {
17541768

17551769
/// Pushes an [`EntityCommand`] to the queue, which will get executed for the current [`Entity`].
17561770
///
1757-
/// If the [`EntityCommand`] returns a [`Result`], it will be handled using the [default error handler](error_handler::default).
1771+
/// If the [`EntityCommand`] returns a [`Result`],
1772+
/// it will be handled using the [default error handler](error_handler::default).
17581773
///
17591774
/// To use a custom error handler, see [`EntityCommands::queue_handled`].
17601775
///
@@ -1788,7 +1803,9 @@ impl<'a> EntityCommands<'a> {
17881803
}
17891804

17901805
/// Pushes an [`EntityCommand`] to the queue, which will get executed for the current [`Entity`].
1791-
/// If the command returns a [`Result`] the given `error_handler` will be used to handle error cases.
1806+
///
1807+
/// If the [`EntityCommand`] returns a [`Result`],
1808+
/// the given `error_handler` will be used to handle error cases.
17921809
///
17931810
/// To implicitly use the default error handler, see [`EntityCommands::queue`].
17941811
///

0 commit comments

Comments
 (0)