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

bevy_core: Derive useful traits on FrameCount #13291

Merged
merged 5 commits into from
May 10, 2024

Conversation

snendev
Copy link
Contributor

@snendev snendev commented May 9, 2024

Objective

I am emboldened by my last small PR and am here with another.

  • Describe the objective or issue this PR addresses.

It would be nice if FrameCount could be used by downstream plugins that want to use frame data. The example that I have in mind is leafwing_input_playback which has a duplicate implementation of FrameCount used in several structs which rely on those derives (or otherwise the higher-level structs would have to implement these traits manually). That crate, using FrameCount, tracks input frames and timestamps and enables various playback modes.

I am aware that bevy org refrains from deriving lots of unnecessary stuff on bevy types to avoid compile time creep. It is worth mentioning the (equally reasonable) alternative that downstream crates should implement some FrameCount themselves if they want special behavior from it.

Solution

  • Describe the solution used to achieve the objective above.

I added derives for PartialEq, Eq, PartialOrd, Ord and implementations for serde::{Deserialize, Serialize} to FrameCount.

Testing

Manually confirmed that the serde implementation works, but that's all. Let me know if I should do more here.

@snendev
Copy link
Contributor Author

snendev commented May 9, 2024

Didn't quite do my due diligence here, my bad. bevy_core doesn't use serde's derive feature and also has a specific module for serde implementations, so I added a manual implementation there (that I hope is done correctly, I'm not super familiar).

Going to quickly edit the PR body to reflect these changes, double check that the serde implementation works correctly, and edit the PR body with that info as well

Copy link
Contributor

@bushrat011899 bushrat011899 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the proposed changes, but I just have a couple small concerns around testing and the exact implementation. Thanks for contributing!

crates/bevy_core/src/serde.rs Outdated Show resolved Hide resolved
crates/bevy_core/src/serde.rs Show resolved Hide resolved
@bushrat011899 bushrat011899 added C-Enhancement A new feature A-Core Common functionality for all bevy apps D-Straightforward Simple bug fixes and API improvements, docs, test and examples labels May 9, 2024
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see a comment in the code base about why you chose to use a manual serde impl (a decision I agree with!) but once that's done this LGTM :)

@snendev
Copy link
Contributor Author

snendev commented May 9, 2024

I'd like to see a comment in the code base about why you chose to use a manual serde impl

I can't really provide any reasoning -- I was just following the existing pattern in the crate. The traits were implemented manually for Name so serde's derive feature isn't included at this point... changing a dependency seemed like a "bigger" change than this PR was intended to be. I'm happy to add a comment if that's helpful, maybe near the top of the file. Is there a good explanation I should use?

@alice-i-cecile
Copy link
Member

There's my understanding of the motivation here :) I think on the balance I probably prefer to just swap to derives, but I don't mind it. Name needs a custom impl because it's effectively just storing a string but has perf-optimized internals.

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
@snendev
Copy link
Contributor Author

snendev commented May 9, 2024

got it, thanks a bunch alice! makes sense to me

#[test]
fn test_serde_name() {
let name = Name::new("MyComponent");
assert_tokens(&name, &[Token::String("MyComponent")]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lovely that assert_tokens tests serialisation and deserialisation at the same time.

use serde_test::{assert_tokens, Token};

#[test]
fn test_serde_name() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call to test this as well!

@alice-i-cecile alice-i-cecile added this pull request to the merge queue May 10, 2024
@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label May 10, 2024
Merged via the queue into bevyengine:main with commit 4b61bbe May 10, 2024
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Core Common functionality for all bevy apps C-Enhancement A new feature D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants