Use impl Into<A> for Assets::add#10878
Merged
alice-i-cecile merged 2 commits intobevyengine:mainfrom Jan 8, 2024
Merged
Conversation
alice-i-cecile
approved these changes
Dec 9, 2023
Member
I agree with this perspective. Nice change! |
NthTensor
approved these changes
Jan 7, 2024
Contributor
NthTensor
left a comment
There was a problem hiding this comment.
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.
mockersf
approved these changes
Jan 7, 2024
github-merge-queue bot
pushed a commit
that referenced
this pull request
Jan 9, 2024
# Objective A few of these were missed in #10878 ## Solution Fix em
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 thatCubeshould be turned into aMeshbecause 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::addtake animpl Into<A>instead ofA.Solution
Assets::addnow 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::fromandintowere being used arbitrarily even in the same file. This also gets rid of some lines of code because formatting is nicer.Changelog
Assets::addnow takes animpl Into<A>instead ofAT::from(K)orK.into()when adding assetsMigration Guide
Some
intocalls that worked previously might now be broken because of the new trait bounds. You need to either removeintoor perform the conversion explicitly withfrom:Concerns
I believe the primary concerns might be:
Previously, the two APIs were using
intoorfrom, and now it's "nothing" orfrom. You could argue thatintois slightly more explicit than "nothing" in cases like the earlier examples where aColorgets converted to e.g. aStandardMaterial, but I personally don't thinkintoadds 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
intocalls 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 manyintocalls just highlights that the API could be made nicer, and I'd gladly migrate my own projects for it.