Skip to content
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

Added #[proto_comp(with=...)] and #[proto_comp(into=...)] attributes #8

Merged
merged 1 commit into from
Jan 16, 2022

Conversation

MrGVSV
Copy link
Owner

@MrGVSV MrGVSV commented Jan 16, 2022

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 implementing ProtoComponent 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 to clone() will just use Copy 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 derive Clone or implement it themselves.

Therefore, the biggest change is that ProtoComponent structs must derive/implement Clone, unless they implement ProtoComponent 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

  • Added #[proto_comp(into = "ActualComponent")]
  • Added #[proto_comp(with = "my_function")]
  • Added new example - attributes
  • Added enum support
  • Deriving ProtoComponent now requires Clone

@MrGVSV MrGVSV self-assigned this Jan 16, 2022
@MrGVSV MrGVSV merged commit a20b015 into main Jan 16, 2022
@MrGVSV MrGVSV deleted the derive-attributes branch January 16, 2022 02:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant