@@ -15,7 +15,7 @@ use crate::{
15
15
component:: { Component , ComponentId , ComponentInfo } ,
16
16
entity:: { Entity , EntityCloneBuilder } ,
17
17
event:: Event ,
18
- system:: { error_handler, Command , IntoObserverSystem } ,
18
+ system:: { error_handler, Command , HandleError , IntoObserverSystem } ,
19
19
world:: { error:: EntityFetchError , EntityWorldMut , FromWorld , World } ,
20
20
} ;
21
21
use bevy_ptr:: OwningPtr ;
@@ -86,23 +86,34 @@ pub trait EntityCommand<T = ()>: Send + 'static {
86
86
pub trait CommandWithEntity < T > {
87
87
/// Passes in a specific entity to an [`EntityCommand`], resulting in a [`Command`] that
88
88
/// internally runs the [`EntityCommand`] on that entity.
89
- fn with_entity ( self , entity : Entity ) -> impl Command < T > ;
89
+ fn with_entity ( self , entity : Entity ) -> impl Command < T > + HandleError < T > ;
90
90
}
91
91
92
- impl < C : EntityCommand < ( ) > > CommandWithEntity < Result < ( ) , EntityFetchError > > for C {
93
- fn with_entity ( self , entity : Entity ) -> impl Command < Result < ( ) , EntityFetchError > > {
94
- move |world : & mut World | {
92
+ impl < C : EntityCommand > CommandWithEntity < Result < ( ) , EntityFetchError > > for C {
93
+ fn with_entity (
94
+ self ,
95
+ entity : Entity ,
96
+ ) -> impl Command < Result < ( ) , EntityFetchError > > + HandleError < Result < ( ) , EntityFetchError > >
97
+ {
98
+ move |world : & mut World | -> Result < ( ) , EntityFetchError > {
95
99
let entity = world. get_entity_mut ( entity) ?;
96
100
self . apply ( entity) ;
97
101
Ok ( ( ) )
98
102
}
99
103
}
100
104
}
101
105
102
- impl < C : EntityCommand < Result < T , Err > > , T , Err > CommandWithEntity < Result < T , EntityCommandError < Err > > >
103
- for C
106
+ impl <
107
+ C : EntityCommand < Result < T , Err > > ,
108
+ T ,
109
+ Err : core:: fmt:: Debug + core:: fmt:: Display + Send + Sync + ' static ,
110
+ > CommandWithEntity < Result < T , EntityCommandError < Err > > > for C
104
111
{
105
- fn with_entity ( self , entity : Entity ) -> impl Command < Result < T , EntityCommandError < Err > > > {
112
+ fn with_entity (
113
+ self ,
114
+ entity : Entity ,
115
+ ) -> impl Command < Result < T , EntityCommandError < Err > > > + HandleError < Result < T , EntityCommandError < Err > > >
116
+ {
106
117
move |world : & mut World | {
107
118
let entity = world. get_entity_mut ( entity) ?;
108
119
match self . apply ( entity) {
@@ -115,7 +126,7 @@ impl<C: EntityCommand<Result<T, Err>>, T, Err> CommandWithEntity<Result<T, Entit
115
126
116
127
/// Takes a [`EntityCommand`] that returns a Result and uses a given error handler function to convert it into
117
128
/// a [`EntityCommand`] that internally handles an error if it occurs and returns `()`.
118
- pub trait HandleEntityError {
129
+ pub trait HandleEntityError < T = ( ) > {
119
130
/// Takes a [`EntityCommand`] that returns a Result and uses a given error handler function to convert it into
120
131
/// a [`EntityCommand`] that internally handles an error if it occurs and returns `()`.
121
132
fn handle_error_with (
@@ -132,7 +143,9 @@ pub trait HandleEntityError {
132
143
}
133
144
}
134
145
135
- impl < C : EntityCommand < crate :: result:: Result > > HandleEntityError for C {
146
+ impl < C : EntityCommand < crate :: result:: Result < T , E > > , T , E : Into < crate :: result:: Error > >
147
+ HandleEntityError < crate :: result:: Result < T , E > > for C
148
+ {
136
149
fn handle_error_with (
137
150
self ,
138
151
error_handler : fn ( & mut World , crate :: result:: Error ) ,
@@ -146,20 +159,27 @@ impl<C: EntityCommand<crate::result::Result>> HandleEntityError for C {
146
159
// SAFETY: location has not changed and entity is valid
147
160
match self . apply ( unsafe { EntityWorldMut :: new ( world, id, location) } ) {
148
161
Ok ( _) => { }
149
- Err ( err) => ( error_handler) ( world, err) ,
162
+ Err ( err) => ( error_handler) ( world, err. into ( ) ) ,
150
163
}
151
164
}
152
165
}
153
166
}
154
167
155
- /// Takes a [`EntityCommand`] that returns a [`Result`] with an error that can be converted into the [`Error`] type
156
- /// and returns a [`EntityCommand`] that internally converts that error to [`Error`] (if it occurs).
157
- pub fn map_entity_command_err < T , E : Into < crate :: result:: Error > > (
158
- command : impl EntityCommand < Result < T , E > > ,
159
- ) -> impl EntityCommand < Result < T , crate :: result:: Error > > {
160
- move |entity : EntityWorldMut | match command. apply ( entity) {
161
- Ok ( result) => Ok ( result) ,
162
- Err ( err) => Err ( err. into ( ) ) ,
168
+ impl < C : EntityCommand > HandleEntityError for C {
169
+ #[ inline]
170
+ fn handle_error_with (
171
+ self ,
172
+ _error_handler : fn ( & mut World , crate :: result:: Error ) ,
173
+ ) -> impl EntityCommand {
174
+ self
175
+ }
176
+
177
+ #[ inline]
178
+ fn handle_error ( self ) -> impl EntityCommand
179
+ where
180
+ Self : Sized ,
181
+ {
182
+ self
163
183
}
164
184
}
165
185
0 commit comments