MovingPtr<'_,B: Bundle> impls Bundle but with typeid #21454
+185 −54
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.
This is an alternative solution to #20976
Objective
Users using
MovingPtrbundles, or implementing bundle-related types that have to deal withMovingPtrs of bundles currently have to copy the bundle onto the stack before they can spawn or insert it, mostly negating the benefits from #20772.Solution
By implementing
BundleforMovingPtr<B: Bundle>, bundles can be spawned with the existing API, including as parts of other bundles, like(R::from(entity), bundle_ptr).In order to implement
Bundlefor anyMovingPtr<'_, B: Bundle>we have to remove the'staticbound onBundle, which is possible when usingtypeidto get the bundle's unique identifier, as suggested by @james7132.Internally,
typeiduses dynamic dispatch to go from aT: 'ato aT: 'staticto callTypeId::ofon, but it appears as if rustc is able to de-virtualise this call. This should, however, definitely be benchmarked before this PR is merged.A number of users of
BundlerequireBundle + 'staticnow, which means a surprisingly wide diff, including for upgrading users of bevy_ecs. Some of the examples have also revealed that could lead to confusing error messages and requires frequent use ofuse<>when using thefn() -> impl Bundlepattern to prevent rustc from pulling in unrelated lifetimes.This could be simplified by renaming the bundle trait and providing a trait alias
trait Bundle: BikeshedBundleName + 'staticwhich retains the previous bounds onBundle; in this case, only implementers of the Bundle trait would be directly impacted (see #21443 and #19491, which factors out some ofBundleinto another trait, as a reference).Alternatives
#21443