-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[Merged by Bors] - Provide public EntityRef::get_change_ticks_by_id
that takes ComponentId
#6683
Conversation
/// use this in cases where the actual component types are not known at | ||
/// compile time.** | ||
#[inline] | ||
pub fn get_change_ticks_by_id(&self, component_id: ComponentId) -> Option<&'w ComponentTicks> { |
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.
These impls aren't safe. get_ticks
assumes component_id
is valid. get_ticks_with_type
validates this by looking up the ComponentId using the TypeId (and returning None if there is no valid mapping).
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.
Thanks for the feedback @cart
What is the preferred course of action in this case?
I assume it is better to add the following validation, just double-checking before making another commit.
if !self.contains_id(component_id) {
return None;
}
Alternatively, I could make the methods unsafe and delegate the safety validation to the end user.
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.
The other *_by_id
APIs are also all unsafe
and require user validation. It might be best to just mark it unsafe and document the safety invariants.
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.
Strong preference to keep these safe :) The extra expressiveness isn't useful here, just a footgun.
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.
Agree with the choice to validate the ComponentId
and keep the method safe.
Other public by_id
methods are sometimes unsafe
by necessity (like OwningPtr
required to match ComponentId
) but limit the safety requirements to a minimum.
Internal unsafe helpers usually require component_id
to be valid for performance reasons, and because the surrounding code usually has already checked their validity.
Updated the implementation. Now EntityRef::get_change_ticks_by_id and EntityMut::get_change_ticks_by_id |
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.
Approved, with one suggestion to the docs.
/// use this in cases where the actual component types are not known at | ||
/// compile time.** | ||
#[inline] | ||
pub fn get_change_ticks_by_id(&self, component_id: ComponentId) -> Option<&'w ComponentTicks> { |
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.
Agree with the choice to validate the ComponentId
and keep the method safe.
Other public by_id
methods are sometimes unsafe
by necessity (like OwningPtr
required to match ComponentId
) but limit the safety requirements to a minimum.
Internal unsafe helpers usually require component_id
to be valid for performance reasons, and because the surrounding code usually has already checked their validity.
Thank you for the docs improvement suggestion, committed your adjustment |
Also adds get_change_ticks_by_id for EntityMut
EntityRef::get_change_ticks_by_id and EntityMut::get_change_ticks_by_id will now validate component_id and return None if it is invalid.
…ellermann from code review Co-authored-by: Jakob Hellermann <jakob.hellermann@protonmail.com>
aa68f2a
to
85e1088
Compare
Apply suggestions from code review by JoJoJet Co-authored-by: JoJoJet <21144246+JoJoJet@users.noreply.github.com>
bors r+ |
EntityRef::get_change_ticks_by_id
that takes ComponentId
EntityRef::get_change_ticks_by_id
that takes ComponentId
…entId` (bevyengine#6683) # Objective Fixes bevyengine#6682 ## Solution Add `EntityRef::get_change_ticks_by_id` Add `EntityMut::get_change_ticks_by_id` Co-authored-by: Aleksandr Belkin <sQu1rr@users.noreply.github.com>
…entId` (bevyengine#6683) # Objective Fixes bevyengine#6682 ## Solution Add `EntityRef::get_change_ticks_by_id` Add `EntityMut::get_change_ticks_by_id` Co-authored-by: Aleksandr Belkin <sQu1rr@users.noreply.github.com>
Objective
Fixes #6682
Solution
Add
EntityRef::get_change_ticks_by_id
Add
EntityMut::get_change_ticks_by_id