Add TypeRegistration::on_register#2
Draft
MrGVSV wants to merge 1 commit into
Draft
Conversation
Deprecates `GetTypeRegistration::register_type_dependencies`
816a867 to
7474c6a
Compare
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
Followup to bevyengine#24518.
bevyengine#24518 makes it so that type data can be given
on_insertandon_registercallbacks. This enables users to add dependencies and other registry/registration operations in a much more flexible way.My goal with this PR is to unify that experience for
GetTypeRegistration. This would also allow us to run these callbacks even for a dynamically generatedTypeRegistration(since we normally only runGetTypeRegistration::register_type_dependenciesviaTypeRegistry::registerwhere the type ofTis known at compile time).Solution
This PR deprecates
GetTypeRegistration::register_type_dependenciesin favor of a newTypeRegistration::on_registercallback system.TypeRegistration::on_registeraccepts aFnOnce(&mut TypeRegistry) + Send + Sync + 'staticand runs when a type is registered into aTypeRegistry.I chose to make it so that by default
TypeRegistration::on_registerwill wrap any existing callback, running the old run first then the new one on registration. This was done to prevent footguns where a user attempts to add a callback for aTypeRegistrationcreated viaGetTypeRegistration::get_type_registration, since they'd inadvertently lose any registration callback set by the implementation (including the registration of any type dependencies).Instead, if users want to completely replace the callback, I added
TypeRegistration::replace_on_register.Both methods take
selfand returnSelf, thus adding some amount of assurance that they will not be overwritten by downstream consumers and so that they can be used in the builder pattern for creating aTypeRegistration.This does come at the cost of a
Box(~8 bytes) per registration, but I think the more consistent API is worth it. Though I'm open to closing this out if we feel this is an unnecessary increase in memory consumption.Testing
I added new unit and doc tests in
bevy_reflect.Showcase
A
TypeRegistrationcan now be given a custom on-registration callback directly: