bevy/crates/bevy_ecs/src
Christian Hughes c7ec456e50
Support systems that take references as input (#15184)
# Objective

- Fixes #14924
- Closes #9584

## Solution

- We introduce a new trait, `SystemInput`, that serves as a type
function from the `'static` form of the input, to its lifetime'd
version, similarly to `SystemParam` or `WorldQuery`.
- System functions now take the lifetime'd wrapped version,
`SystemInput::Param<'_>`, which prevents the issue presented in #14924
(i.e. `InRef<T>`).
- Functions for running systems now take the lifetime'd unwrapped
version, `SystemInput::Inner<'_>` (i.e. `&T`).
- Due to the above change, system piping had to be re-implemented as a
standalone type, rather than `CombinatorSystem` as it was previously.
- Removes the `Trigger<'static, E, B>` transmute in observer runner
code.

## Testing

- All current tests pass.
- Added additional tests and doc-tests.

---

## Showcase

```rust
let mut world = World::new();

let mut value = 2;

// Currently possible:
fn square(In(input): In<usize>) -> usize {
    input * input
}
value = world.run_system_once_with(value, square);

// Now possible:
fn square_mut(InMut(input): InMut<usize>) {
    *input *= *input;
}
world.run_system_once_with(&mut value, square_mut);

// Or:
fn square_ref(InRef(input): InRef<usize>) -> usize {
    *input * *input
}
value = world.run_system_once_with(&value, square_ref);
```

## Migration Guide

- All current explicit usages of the following types must be changed in
the way specified:
    - `SystemId<I, O>` to `SystemId<In<I>, O>`
    - `System<In = T>` to `System<In = In<T>>`
    - `IntoSystem<I, O, M>` to `IntoSystem<In<I>, O, M>`
    - `Condition<M, T>` to `Condition<M, In<T>>`
- `In<Trigger<E, B>>` is no longer a valid input parameter type. Use
`Trigger<E, B>` directly, instead.

---------

Co-authored-by: Giacomo Stevanato <giaco.stevanato@gmail.com>
2024-09-23 17:37:29 +00:00
..
entity Choose more descriptive field names for ReserveEntitiesIterator (#15168) 2024-09-20 19:13:35 +00:00
event Rename Add to Queue for methods with deferred semantics (#15234) 2024-09-17 00:17:49 +00:00
identifier Migrated NonZero* to NonZero<*> (#14978) 2024-08-30 02:37:47 +00:00
observer Support systems that take references as input (#15184) 2024-09-23 17:37:29 +00:00
query Enable clippy::check-private-items so that missing_safety_doc will apply to private functions as well (#15161) 2024-09-18 15:28:41 +00:00
reflect Rename Add to Queue for methods with deferred semantics (#15234) 2024-09-17 00:17:49 +00:00
schedule Support systems that take references as input (#15184) 2024-09-23 17:37:29 +00:00
storage simplify std::mem references (#15315) 2024-09-19 21:28:16 +00:00
system Support systems that take references as input (#15184) 2024-09-23 17:37:29 +00:00
world Allow animation clips to animate arbitrary properties. (#15282) 2024-09-23 17:14:12 +00:00
archetype.rs Add EntityRefExcept and EntityMutExcept world queries, in preparation for generalized animation. (#15207) 2024-09-17 14:53:39 +00:00
batching.rs Parallel event reader (#12554) 2024-04-22 16:37:42 +00:00
bundle.rs Enable clippy::check-private-items so that missing_safety_doc will apply to private functions as well (#15161) 2024-09-18 15:28:41 +00:00
change_detection.rs change return type of World::resource_ref to Ref (#15263) 2024-09-21 19:11:13 +00:00
component.rs Enable clippy::check-private-items so that missing_safety_doc will apply to private functions as well (#15161) 2024-09-18 15:28:41 +00:00
intern.rs Moves intern and label modules into bevy_ecs (#12772) 2024-04-08 15:34:11 +00:00
label.rs Add mappings to EntityMapper (#13727) 2024-06-08 12:52:23 +00:00
lib.rs Support systems that take references as input (#15184) 2024-09-23 17:37:29 +00:00
removal_detection.rs Created an EventMutator for when you want to mutate an event before reading (#13818) 2024-07-08 14:53:06 +00:00
traversal.rs Minimal Bubbling Observers (#13991) 2024-07-15 13:39:41 +00:00