Skip to content

A Bevy CombinatorSystem extension for systems that output Result types

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

snendev-labs/bevy_mod_try_system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bevy_mod_try_system

Crates.io Docs

This crate defines TrySystemExt, an extension trait implemented on all IntoSystem with Out = Result<Val, Error>. It provides a method, pipe_err, which accepts self and another system (with In = Error) as parameters, and returns a CombinerSystem (the same vehicle behind PipeSystem and the system.pipe method) that passes errors from the first system into the second system.

Warnings about chained outputs.

Assume we intend to call system_a.pipe_err(system_b), where system_a returns some type Result<(), Error>. If we wanted to pipe some output Value from system_a out to a third system_c, we can, as long as system B can provide fallback values of Value when system A returns an Error:

fn system_a() -> Result<Value, Error> {
    // ...
}

fn system_b(In(error): In<Error>) -> Value {
    // ...
}

fn system_c(In(value): In<Value>) {
    // ...
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Update, system_a.pipe_err(system_b).pipe(system_c))
        .run();
}

Importantly, if system_b cannot provide a fallback value, then this doesn't work very well. However, you can usually be clever, for example returning Option<Value> from system_b and using system.map:

// ...
fn system_b(In(error): In<Error>) -> Value {
    // ...
}

// ...
app.add_systems(Update, system_a.map(|result| result.map(|value| Some(value))).pipe_err(system_b).pipe(system_c));

See the tests for more information.

About

A Bevy CombinatorSystem extension for systems that output Result types

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages