Skip to content

Commit

Permalink
Slim down impl, improve type inference, small style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cart committed Jun 9, 2021
1 parent 42a3c7b commit 1e753f8
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 179 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub mod prelude {
Schedule, Stage, StageLabel, State, SystemLabel, SystemSet, SystemStage,
},
system::{
CommandError, CommandErrorHandler, Commands, FallibleCommand, In, IntoChainSystem,
CommandErrorHandler, Commands, FallibleCommand, In, IntoChainSystem,
IntoExclusiveSystem, IntoSystem, Local, NonSend, NonSendMut, Query, QuerySet,
RemovedComponents, Res, ResMut, System,
},
Expand Down
87 changes: 61 additions & 26 deletions crates/bevy_ecs/src/system/commands/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::{
prelude::{FallibleCommand, World},
system::Command,
};
use bevy_utils::tracing::error;
use std::{
fmt::Debug,
ops::{Deref, DerefMut},
};

use super::{handler::*, Command, FallibleCommand};

#[doc(hidden)]
pub trait AddCommand {
fn add_command(&mut self, command: impl Command);
Expand All @@ -19,7 +22,6 @@ pub trait AddCommand {
pub struct FallibleCommandConfig<'a, C, T>
where
C: FallibleCommand,
C::Error: Debug,
T: AddCommand,
{
command: Option<C>,
Expand All @@ -29,7 +31,6 @@ where
impl<'a, C, T> Deref for FallibleCommandConfig<'a, C, T>
where
C: FallibleCommand,
C::Error: Debug,
T: AddCommand,
{
type Target = T;
Expand All @@ -43,7 +44,6 @@ where
impl<'a, C, T> DerefMut for FallibleCommandConfig<'a, C, T>
where
C: FallibleCommand,
C::Error: Debug,
T: AddCommand,
{
#[inline]
Expand All @@ -52,13 +52,59 @@ where
}
}

/// Builtin command error handlers.
pub struct CommandErrorHandler;

impl CommandErrorHandler {
/// If the command failed, log the error.
///
/// ## Note
/// This is the default behavior if no error handler is specified.
pub fn log<E: Debug>(error: E, _world: &mut World) {
error!("Commands failed with error: {:?}", error)
}

/// If the command failed, [`panic!`] with the error.
pub fn panic<E: Debug>(error: E, _world: &mut World) {
panic!("Commands failed with error: {:?}", error)
}

/// If the command failed, ignore the error and silently succeed.
pub fn ignore<E: Debug>(_error: E, _world: &mut World) {}
}

pub(crate) struct HandledErrorCommand<C, F>
where
C: FallibleCommand,
F: FnOnce(C::Error, &mut World) + Send + Sync + 'static,
{
pub(crate) command: C,
pub(crate) error_handler: F,
}

impl<C, F> Command for HandledErrorCommand<C, F>
where
C: FallibleCommand,
F: FnOnce(C::Error, &mut World) + Send + Sync + 'static,
{
fn write(self: Box<Self>, world: &mut World) {
let HandledErrorCommand {
command,
error_handler,
} = *self;

if let Err(error) = command.try_write(world) {
error_handler(error, world);
}
}
}

/// Similar to [`FallibleCommandConfig`] however does not
/// implement [`DerefMut`] nor return `&mut T` of the underlying
/// Commands type.
pub struct FinalFallibleCommandConfig<'a, C, T>
where
C: FallibleCommand,
C::Error: Debug,
T: AddCommand,
{
command: Option<C>,
Expand Down Expand Up @@ -98,19 +144,6 @@ macro_rules! impl_fallible_commands {
C::Error: Debug,
T: AddCommand,
{
/// If the command failed, run the provided `error_handler`.
pub fn on_err_do<H>(&mut self, error_handler: H) -> $returnty
where
H: FnOnce(CommandError<'_, C::Error>) + Send + Sync + 'static,
{
let command = self.command.take().unwrap();
self.inner.add_command(CommandErrorHandlerWrapper {
command,
error_handler,
});
self.$returnfunc()
}

/// If the command failed, run the provided `error_handler`.
///
/// ## Note
Expand All @@ -125,17 +158,20 @@ macro_rules! impl_fallible_commands {
///
/// fn system(mut commands: Commands) {
/// // built-in error handler
/// commands.spawn().insert(42).on_err(CommandErrorHandler::ignore());
/// commands.spawn().insert(42).on_err(CommandErrorHandler::ignore);
///
/// // custom error handler
/// commands.spawn().insert(42).on_err(|_: CommandError<_>| {});
/// commands.spawn().insert(42).on_err(|error, world| {});
/// }
/// ```
pub fn on_err(&mut self, error_handler: impl CommandErrorHandling<C>) -> $returnty {
pub fn on_err(
&mut self,
handler: impl FnOnce(C::Error, &mut World) + Send + Sync + 'static,
) -> $returnty {
let command = self.command.take().unwrap();
self.inner.add_command(CommandErrorHandlerWrapper {
self.inner.add_command(HandledErrorCommand {
command,
error_handler,
error_handler: handler,
});
self.$returnfunc()
}
Expand All @@ -144,12 +180,11 @@ macro_rules! impl_fallible_commands {
impl<'a, C, T> Drop for $name<'a, C, T>
where
C: FallibleCommand,
C::Error: Debug,
T: AddCommand,
{
fn drop(&mut self) {
if self.command.is_some() {
self.on_err(CommandErrorHandler::log());
self.on_err(CommandErrorHandler::log);
}
}
}
Expand Down
93 changes: 0 additions & 93 deletions crates/bevy_ecs/src/system/commands/handler.rs

This file was deleted.

Loading

0 comments on commit 1e753f8

Please sign in to comment.