bevy_reflect: Add clone registrations project-wide #18307
Merged
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
Now that #13432 has been merged, it's important we update our reflected types to properly opt into this feature. If we do not, then this could cause issues for users downstream who want to make use of reflection-based cloning.
Solution
This PR is broken into 4 commits:
Add
#[reflect(Clone)]
on all types marked#[reflect(opaque)]
that are alsoClone
. This is mandatory as these types would otherwise cause the cloning operation to fail for any type that contains it at any depth.Update the reflection example to suggest adding
#[reflect(Clone)]
on opaque types.Add
#[reflect(clone)]
attributes on all fields marked#[reflect(ignore)]
that are alsoClone
. This prevents the ignored field from causing the cloning operation to fail.Note that some of the types that contain these fields are also
Clone
, and thus can be marked#[reflect(Clone)]
. This makes the#[reflect(clone)]
attribute redundant. However, I think it's safer to keep it marked in the case that theClone
impl/derive is ever removed. I'm open to removing them, though, if people disagree.Finally, I added
#[reflect(Clone)]
on all types that are alsoClone
. While not strictly necessary, it enables us to reduce the generated output since we can just callClone::clone
directly instead of callingPartialReflect::reflect_clone
on each variant/field. It also means we benefit from any optimizations or customizations made in theClone
impl, including directly dereferencingCopy
values and increasing reference counters.Along with that change I also took the liberty of adding any missing registrations that I saw could be applied to the type as well, such as
Default
,PartialEq
, andHash
. There were hundreds of these to edit, though, so it's possible I missed quite a few.That last commit is massive. There were nearly 700 types to update. So it's recommended to review the first three before moving onto that last one.
Additionally, I can break the last commit off into its own PR or into smaller PRs, but I figured this would be the easiest way of doing it (and in a timely manner since I unfortunately don't have as much time as I used to for code contributions).
Testing
You can test locally with a
cargo check
: