@@ -5,8 +5,8 @@ use crate::{
5
5
component:: { ComponentId , Tick } ,
6
6
error:: Result ,
7
7
query:: { Access , FilteredAccessSet } ,
8
- system:: { input:: SystemIn , BoxedSystem , System } ,
9
- world:: { unsafe_world_cell:: UnsafeWorldCell , DeferredWorld , World } ,
8
+ system:: { input:: SystemIn , BoxedSystem , System , SystemInput } ,
9
+ world:: { unsafe_world_cell:: UnsafeWorldCell , DeferredWorld , FromWorld , World } ,
10
10
} ;
11
11
12
12
use super :: { IntoSystem , SystemParamValidationError } ;
@@ -118,5 +118,238 @@ impl<S: System<In = ()>> System for InfallibleSystemWrapper<S> {
118
118
}
119
119
}
120
120
121
+ /// See [`IntoSystem::with_input`] for details.
122
+ pub struct WithInputWrapper < S , T >
123
+ where
124
+ for < ' i > S : System < In : SystemInput < Inner < ' i > = & ' i mut T > > ,
125
+ T : Send + Sync + ' static ,
126
+ {
127
+ system : S ,
128
+ value : T ,
129
+ }
130
+
131
+ impl < S , T > WithInputWrapper < S , T >
132
+ where
133
+ for < ' i > S : System < In : SystemInput < Inner < ' i > = & ' i mut T > > ,
134
+ T : Send + Sync + ' static ,
135
+ {
136
+ /// Wraps the given system with the given input value.
137
+ pub fn new < M > ( system : impl IntoSystem < S :: In , S :: Out , M , System = S > , value : T ) -> Self {
138
+ Self {
139
+ system : IntoSystem :: into_system ( system) ,
140
+ value,
141
+ }
142
+ }
143
+
144
+ /// Returns a reference to the input value.
145
+ pub fn value ( & self ) -> & T {
146
+ & self . value
147
+ }
148
+
149
+ /// Returns a mutable reference to the input value.
150
+ pub fn value_mut ( & mut self ) -> & mut T {
151
+ & mut self . value
152
+ }
153
+ }
154
+
155
+ impl < S , T > System for WithInputWrapper < S , T >
156
+ where
157
+ for < ' i > S : System < In : SystemInput < Inner < ' i > = & ' i mut T > > ,
158
+ T : Send + Sync + ' static ,
159
+ {
160
+ type In = ( ) ;
161
+
162
+ type Out = S :: Out ;
163
+
164
+ fn name ( & self ) -> Cow < ' static , str > {
165
+ self . system . name ( )
166
+ }
167
+
168
+ fn component_access ( & self ) -> & Access < ComponentId > {
169
+ self . system . component_access ( )
170
+ }
171
+
172
+ fn component_access_set ( & self ) -> & FilteredAccessSet < ComponentId > {
173
+ self . system . component_access_set ( )
174
+ }
175
+
176
+ fn archetype_component_access ( & self ) -> & Access < ArchetypeComponentId > {
177
+ self . system . archetype_component_access ( )
178
+ }
179
+
180
+ fn is_send ( & self ) -> bool {
181
+ self . system . is_send ( )
182
+ }
183
+
184
+ fn is_exclusive ( & self ) -> bool {
185
+ self . system . is_exclusive ( )
186
+ }
187
+
188
+ fn has_deferred ( & self ) -> bool {
189
+ self . system . has_deferred ( )
190
+ }
191
+
192
+ unsafe fn run_unsafe (
193
+ & mut self ,
194
+ _input : SystemIn < ' _ , Self > ,
195
+ world : UnsafeWorldCell ,
196
+ ) -> Self :: Out {
197
+ self . system . run_unsafe ( & mut self . value , world)
198
+ }
199
+
200
+ fn apply_deferred ( & mut self , world : & mut World ) {
201
+ self . system . apply_deferred ( world) ;
202
+ }
203
+
204
+ fn queue_deferred ( & mut self , world : DeferredWorld ) {
205
+ self . system . queue_deferred ( world) ;
206
+ }
207
+
208
+ unsafe fn validate_param_unsafe (
209
+ & mut self ,
210
+ world : UnsafeWorldCell ,
211
+ ) -> Result < ( ) , SystemParamValidationError > {
212
+ self . system . validate_param_unsafe ( world)
213
+ }
214
+
215
+ fn initialize ( & mut self , world : & mut World ) {
216
+ self . system . initialize ( world) ;
217
+ }
218
+
219
+ fn update_archetype_component_access ( & mut self , world : UnsafeWorldCell ) {
220
+ self . system . update_archetype_component_access ( world) ;
221
+ }
222
+
223
+ fn check_change_tick ( & mut self , change_tick : Tick ) {
224
+ self . system . check_change_tick ( change_tick) ;
225
+ }
226
+
227
+ fn get_last_run ( & self ) -> Tick {
228
+ self . system . get_last_run ( )
229
+ }
230
+
231
+ fn set_last_run ( & mut self , last_run : Tick ) {
232
+ self . system . set_last_run ( last_run) ;
233
+ }
234
+ }
235
+
236
+ /// Constructed in [`IntoSystem::with_input_from`].
237
+ pub struct WithInputFromWrapper < S , T > {
238
+ system : S ,
239
+ value : Option < T > ,
240
+ }
241
+
242
+ impl < S , T > WithInputFromWrapper < S , T >
243
+ where
244
+ for < ' i > S : System < In : SystemInput < Inner < ' i > = & ' i mut T > > ,
245
+ T : Send + Sync + ' static ,
246
+ {
247
+ /// Wraps the given system.
248
+ pub fn new < M > ( system : impl IntoSystem < S :: In , S :: Out , M , System = S > ) -> Self {
249
+ Self {
250
+ system : IntoSystem :: into_system ( system) ,
251
+ value : None ,
252
+ }
253
+ }
254
+
255
+ /// Returns a reference to the input value, if it has been initialized.
256
+ pub fn value ( & self ) -> Option < & T > {
257
+ self . value . as_ref ( )
258
+ }
259
+
260
+ /// Returns a mutable reference to the input value, if it has been initialized.
261
+ pub fn value_mut ( & mut self ) -> Option < & mut T > {
262
+ self . value . as_mut ( )
263
+ }
264
+ }
265
+
266
+ impl < S , T > System for WithInputFromWrapper < S , T >
267
+ where
268
+ for < ' i > S : System < In : SystemInput < Inner < ' i > = & ' i mut T > > ,
269
+ T : FromWorld + Send + Sync + ' static ,
270
+ {
271
+ type In = ( ) ;
272
+
273
+ type Out = S :: Out ;
274
+
275
+ fn name ( & self ) -> Cow < ' static , str > {
276
+ self . system . name ( )
277
+ }
278
+
279
+ fn component_access ( & self ) -> & Access < ComponentId > {
280
+ self . system . component_access ( )
281
+ }
282
+
283
+ fn component_access_set ( & self ) -> & FilteredAccessSet < ComponentId > {
284
+ self . system . component_access_set ( )
285
+ }
286
+
287
+ fn archetype_component_access ( & self ) -> & Access < ArchetypeComponentId > {
288
+ self . system . archetype_component_access ( )
289
+ }
290
+
291
+ fn is_send ( & self ) -> bool {
292
+ self . system . is_send ( )
293
+ }
294
+
295
+ fn is_exclusive ( & self ) -> bool {
296
+ self . system . is_exclusive ( )
297
+ }
298
+
299
+ fn has_deferred ( & self ) -> bool {
300
+ self . system . has_deferred ( )
301
+ }
302
+
303
+ unsafe fn run_unsafe (
304
+ & mut self ,
305
+ _input : SystemIn < ' _ , Self > ,
306
+ world : UnsafeWorldCell ,
307
+ ) -> Self :: Out {
308
+ let value = self
309
+ . value
310
+ . as_mut ( )
311
+ . expect ( "System input value was not found. Did you forget to initialize the system before running it?" ) ;
312
+ self . system . run_unsafe ( value, world)
313
+ }
314
+
315
+ fn apply_deferred ( & mut self , world : & mut World ) {
316
+ self . system . apply_deferred ( world) ;
317
+ }
318
+
319
+ fn queue_deferred ( & mut self , world : DeferredWorld ) {
320
+ self . system . queue_deferred ( world) ;
321
+ }
322
+
323
+ unsafe fn validate_param_unsafe (
324
+ & mut self ,
325
+ world : UnsafeWorldCell ,
326
+ ) -> Result < ( ) , SystemParamValidationError > {
327
+ self . system . validate_param_unsafe ( world)
328
+ }
329
+
330
+ fn initialize ( & mut self , world : & mut World ) {
331
+ self . system . initialize ( world) ;
332
+ if self . value . is_none ( ) {
333
+ self . value = Some ( T :: from_world ( world) ) ;
334
+ }
335
+ }
336
+
337
+ fn update_archetype_component_access ( & mut self , world : UnsafeWorldCell ) {
338
+ self . system . update_archetype_component_access ( world) ;
339
+ }
340
+
341
+ fn check_change_tick ( & mut self , change_tick : Tick ) {
342
+ self . system . check_change_tick ( change_tick) ;
343
+ }
344
+
345
+ fn get_last_run ( & self ) -> Tick {
346
+ self . system . get_last_run ( )
347
+ }
348
+
349
+ fn set_last_run ( & mut self , last_run : Tick ) {
350
+ self . system . set_last_run ( last_run) ;
351
+ }
352
+ }
353
+
121
354
/// Type alias for a `BoxedSystem` that a `Schedule` can store.
122
355
pub type ScheduleSystem = BoxedSystem < ( ) , Result > ;
0 commit comments