bevyengine / bevyengine/bevy

Default piping via system return type

Open
#23,666 2 comments 0 reactions 0 assignees View on GitHub
A-ECS C-Usability D-Modest S-Ready-For-Implementation X-Contentious
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

## What problem does this solve or what need does it fill?
Plenty of time I end up using `let Ok(...) else { return; }` and such for early returning systems where I just want no-op without the required state I want. This isn't too bad but it bloats the code/makes it harder to read, which is where the `?` operator comes in handy. And then we can pipe to `ignore`. However I think we might be able to go slightly further than this in terms of ease of use.

## What solution would you like?
Define 'default pipes' for the return type of a system. For example if I define a system like:
```rust
fn plugin(app: &mut App) {
app.add_systems(Update, my_system);
}

fn my_system(arg1: Option>, mut arg2: Query<&mut MyComponent>)
-> Result<_, _, ignore> // custom Result with a third generic for "pipe function"
{
let arg1 = arg1?;
let arg2 = arg2.get_mut(arg1.entity)?;
// do stuff
}
```
This would be the equivalent of:
```rust
fn plugin(app: &mut App) {
app.add_systems(Update, my_system.pipe(ignore));
}

fn my_system(arg1: Option>, mut arg2: Query<&mut MyComponent>) -> Result<_, _>
{
let arg1 = arg1?;
let arg2 = arg2.get_mut(arg1.entity)?;
// do stuff
}
```

## What alternative(s) have you considered?
Just use the normal piping when we add the system to the world. Or maybe implement a default response for a BevyError? I'm not sure. It might be nice to be able to specify different responses based on the error we returned, e.g. not having this resource is fine, but if the entity doesn't have this component thats an invariant.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.