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
MovingPtr
bundles, or implementing bundle-related types that have to deal withMovingPtr
s 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
Bundle
forMovingPtr<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
Bundle
for anyMovingPtr<'_, B: Bundle>
we have to remove the'static
bound onBundle
, which is possible when usingtypeid
to get the bundle's unique identifier, as suggested by @james7132.Internally,
typeid
uses dynamic dispatch to go from aT: 'a
to aT: 'static
to callTypeId::of
on, 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
Bundle
requireBundle + 'static
now, 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 Bundle
pattern 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 + 'static
which 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 ofBundle
into another trait, as a reference).Alternatives
#21443