Thoughts on ecs::schedule::StateError
and StateError::AlreadyInState
#7484
stephenmartindale
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Once again, I find myself wrangling with
ecs::schedule::StateError
and I have a lot of thoughts and ideas for potential changes and merge-requests – all of which I would happily code up and submit but, because this is probably worthy of debate, I thought I would start a discussion, first.Trivialities
First, here are some trivialities:
StateError
should be in the prelude. → ecs::schedule::StateError now Clone, PartialEq, Eq; added to prelude. #7486StateError
, as a fieldless error-kindenum
that is alreadypub
, should#[derive(PartialEq, Eq)]
at least. → ecs::schedule::StateError now Clone, PartialEq, Eq; added to prelude. #7486StateError::AlreadyInState
Most of the existing references and uses of
StateError
are inbevy_ecs/src/schedule/state.rs
where it is used forpub fn
s that mostly returnResult<(), StateError>
.In my opinion, a wide change to all that public API that would make it much better would be:
StateError::AlreadyInState
pub fn
signatures toResult<bool, StateError>
, where:Ok(false)
is returned if the requested state-change was a NO-OP – i.e. the case where the existing code would returnStateError::AlreadyInState
Ok(true)
would be returned if the state-change proceeded – the existing code would returnOk(())
If one contemplates that, it immediately becomes obvious that many of the functions would no longer need to return
Result<_, _>
at all because, exceptingStateError::AlreadyInState
, they don't error. These include:pub fn overwrite_set()
pub fn overwrite_replace()
pub fn overwrite_push()
All of those, above, could just return
bool
, notResult
.Also, there's
pub fn pop()
which currently returnsResult<(), StateError>
but cannot ever returnErr(StateError::AlreadyInState)
so, in the strictest terms, it actually shouldn't useStateError
as it stands but rather someStateError-{AlreadyInState}
. After my proposed refactoring, it could continue to returnResult<(), StateError>
and that would become [more] correct.Possible Regressions
None. This would be a breaking change but no functionality would be lost – callers would still be able to distinguish whether a state-related operation was successful, a NO-OP, or a failure.
Argumentative Code Snippets
Consider two call-site cases:
Currently, call-site code might look like this:
If my proposal grants approval:
Personally, I like how symmetrical both those two cases are in the second snippet. (Of course,
.expect()
or arguments toassert!
or any myriad mechanisms exist to make nicer errors and panics.)Outlook
As I said, I am really keen to code up and submit merge-requests for all of the changes I propose – with documentation.
Let me know your opinions and concerns and whether that would be worth the bother – this is an API-breaking-change so I'm starting with a discussion before I start with hacking.
Beta Was this translation helpful? Give feedback.
All reactions