Use impl Into<A>
for Assets::add
#10878
Merged
+247
−268
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.
Motivation
When spawning entities into a scene, it is very common to create assets like meshes and materials and to add them via asset handles. A common setup might look like this:
Let's take a closer look at the part that adds the assets using
add
.Here, "mesh" and "material" are both repeated three times. It's very explicit, but I find it to be a bit verbose. In addition to being more code to read and write, the extra characters can sometimes also lead to the code being formatted to span multiple lines even though the core task, adding e.g. a primitive mesh, is extremely simple.
A way to address this is by using
.into()
:This is fine, but from the names and the type of
meshes
, we already know what the type should be. It's very clear thatCube
should be turned into aMesh
because of the context it's used in..into()
is just seven characters, but it's so common that it quickly adds up and gets annoying.It would be nice if you could skip all of the conversion and let Bevy handle it for you:
Objective
Make adding assets more ergonomic by making
Assets::add
take animpl Into<A>
instead ofA
.Solution
Assets::add
now takes animpl Into<A>
instead ofA
, so e.g. this works:I also changed all examples to use this API, which increases consistency as well because
Mesh::from
andinto
were being used arbitrarily even in the same file. This also gets rid of some lines of code because formatting is nicer.Changelog
Assets::add
now takes animpl Into<A>
instead ofA
T::from(K)
orK.into()
when adding assetsMigration Guide
Some
into
calls that worked previously might now be broken because of the new trait bounds. You need to either removeinto
or perform the conversion explicitly withfrom
:Concerns
I believe the primary concerns might be:
Previously, the two APIs were using
into
orfrom
, and now it's "nothing" orfrom
. You could argue thatinto
is slightly more explicit than "nothing" in cases like the earlier examples where aColor
gets converted to e.g. aStandardMaterial
, but I personally don't thinkinto
adds much value even in this case, and you could still see the actual type from the asset type.As for codegen bloat, I doubt it adds that much, but I'm not very familiar with the details of codegen. I personally value the user-facing code reduction and ergonomics improvements that these changes would provide, but it might be worth checking the other effects in more detail.
Another slight concern is migration pain; apps might have a ton of
into
calls that would need to be removed, and it did take me a while to do so for Bevy itself (maybe around 20-40 minutes). However, I think the fact that there are so manyinto
calls just highlights that the API could be made nicer, and I'd gladly migrate my own projects for it.