Allow systems to return results#8705
Closed
joseph-gio wants to merge 11 commits intobevyengine:mainfrom
Closed
Conversation
alice-i-cecile
requested changes
May 29, 2023
Member
alice-i-cecile
left a comment
There was a problem hiding this comment.
I really like this, but it's hard to discover. Can you add a quick handling_errors_in_systems example in bevy_ecs that demonstrates this, one of the built in logging adaptors and a custom adaptor?
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
alice-i-cecile
approved these changes
May 31, 2023
Member
Author
|
I'm closing this out. Using If anyone finds a way of improving the panic message reporting, feel free to make a new PR based off of this. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Objective
When writing an app in rust, the
mainfunction usually returns nothing, but it can also return aResult; when doing this, returning anErrvalue will result in a panic. This makes writing fallible code much more ergonomic, since?can be used to propagate errors.This flexibility should be extended to systems: users should be able to return a
Resultfrom their systems. This can be done by callingsystem.pipe(unwrap)when adding the system, however this approach requires more boilerplate, incurs a runtime overhead due to thePipeSystem, removes context from the error message (since the panic message has no way of accessing the system's name), and makes the name of the combined system messier (which is stored by allocating a newString).Solution
A system can only be added to a schedule if it implements
IntoSystemConfigs<>. Currently, this is only implemented for systems that take no input and return nothing. Now, this trait is also implemented for any systems that returnResult<(), impl Debug>. A custom system adapter is used that panics when the system returns an error and includes the system name in the message.Initially, I was concerned that this may make the compile error for systems with invalid return types more confusing. However in my testing, the error message does not seem to be any worse.
Changelog
Result<(), E>, whereEis any type that implementsstd::fmt::Debug.