-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use impl Into<A>
for Assets::add
#10878
Use impl Into<A>
for Assets::add
#10878
Conversation
I agree with this perspective. Nice change! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I much prefer this API. Wonder if there are any other places we could use impl Into
. Didn't review every single one of the examples in detail, but the CI says you are good.
# Objective A few of these were missed in #10878 ## Solution Fix em
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.