Added #[proto_comp(with=...)]
and #[proto_comp(into=...)]
attributes
#8
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.
Based on this comment, added the following derive helper attributes for
ProtoComponent
:#[proto_comp(into = "ActualComponent")]
Allows a
From<T>
to be used to created the inserted component. This is helpful for components that need a bit of setup (serialization isn't one-to-one), or for components that a user does not own (i.e., from external crates).#[proto_comp(with = "my_function")]
Allows a common function to be used to generate or insert components. For
ProtoComponent
structs that share some common trait or otherwise can be inserted a certain way, this attribute allows that to be done in a clean and efficient way (without implementingProtoComponent
manually for each type).New Examples
Added the
attributes
example to show off these new attributes.API Changes
This was not only one of my first Bevy projects, but also one of my first Rust projects! I've learned a lot since then (and I'm still learning now). And coming back to this code, I found some parts that could be improved.
But the ones that stood out to me as "definitely remove" were the
#[proto_comp(Copy)]
and#[proto_comp(Clone)]
field attributes. I realize now, that having a#[proto_comp(Copy)]
attribute is almost pointless since a call toclone()
will just useCopy
internally.Additionally, by cloning fields individually, we prevent the user from adding additional functionality/logic when cloning a
ProtoComponent
. It makes much more sense to just clone the entire struct, allowing users to either deriveClone
or implement it themselves.Therefore, the biggest change is that
ProtoComponent
structs must derive/implementClone
, unless they implementProtoComponent
manually. This was already required in a sense, but now it's more explicit.Doing this also allows us to now support enums!
TL;DR
#[proto_comp(into = "ActualComponent")]
#[proto_comp(with = "my_function")]
attributes
ProtoComponent
now requiresClone