-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Safer dynamic event handling for observers #14919
base: main
Are you sure you want to change the base?
Conversation
c760845
to
a233696
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cursory look over, but will do a full review at some point. Approach is very clever, I am very surprised this works and is compatible with propagation. Nice work.
There is still no way to safely access the actual instantiation of a DynamicEvent
? Was that in the original? Have we considered returning a Box<dyn Reflect>
?
This reverts commit bb330e0.
I'm working on adding a separate |
Cool! Nah, keep it separate imo. This is complete. I will approve as long as it does what it says on the tin. |
// SAFETY: DynamicEvent itself does not perform any unsafe operations (like casting), so this is safe. | ||
unsafe { self.with_event_unchecked(event) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This safety comment is not entirely clear to me. It doesn't seem to address the safety requirements of the with_event_unchecked
function:
# Safety The type of the `event` [`ComponentId`] _must_ match the actual value of the event passed into the observer system.
In your safety comment there's no mention of the event
ComponentId
or the type of the actual event that will be passed into the observer system.
I guess this is safe because DynamicEvent
can "match" any event type? If that's the case I would like to see clearer safety requirements from with_event_unchecked
that allow for this situation, and a better safety comment here that addresses the requirements from with_event_unchecked
.
@ItsDoot I quite like this, but it's not vital to get this in for 0.15 so I'm bumping to 0.16. Let me know when you've resolved merge conflicts here though and I'll give you a proper review. |
Objective
This is a more scoped version of #14674, with a removal of the "tuple of event types" logic, which is notably controversial. Closes #14649.
Solution
This PR introduces an expansion of trigger-able event data into something slightly more
Query
-like.With this, we can safely implement observers that can be triggered with multiple different event types, which don't necessarily need to access the event data. This new type is called
DynamicEvent
:Testing
Migration Guide
Observer::with_event
was renamed toObserver::with_event_unchecked
. If you were using this function, consider using a new one that has been added under the old name, which isn't markedunsafe
but requires yourTrigger<E>
'sE
type to beDynamicEvent
.