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

Derive From Variant To Variant #246

Merged
merged 1 commit into from May 24, 2023
Merged

Conversation

astrale-sharp
Copy link
Contributor

Closes #245

@astrale-sharp
Copy link
Contributor Author

I have very little interest for deriving something other than a struct or enum, I will not have time to work on it.

The only opinionated bit of this PR (imo)

is that

struct Struct(i32) ;

Struct(4).to_variant() == dict!{"Struct" : 4}
// instead of 
Struct(4).to_variant() == dict!{"Struct" : array![4]}

// and same for 
enum Enum {
        // this variant
        Variant(i32)
        // but not this one
        Variant2(i32,i32)
}

Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

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

Thanks a lot for this! 👍

This only seems to implement ToVariant. Given the PR title, do you plan to also add FromVariant (could also be separate)?

Please avoid prelude for internal implementation and use the original modules, e.g. godot::builtin.

The Godot representation of tuple-structs/enums with only 1 element can be discussed. While a bit inconsistent with multiple elements, those are often specifically used for "newtypes", as such it can make sense to avoid the array. It means of course it's not easy to version them, but maybe we should recommend using named structs/enums if that is a concern.

CI is currently failing, would be nice if you could address those 🙂

godot-macros/src/derive_to_variant.rs Outdated Show resolved Hide resolved
itest/rust/src/derive_variant.rs Outdated Show resolved Hide resolved
itest/rust/src/derive_variant.rs Outdated Show resolved Hide resolved
@Bromeon Bromeon added feature Adds functionality to the library c: core Core components labels Apr 28, 2023
@astrale-sharp
Copy link
Contributor Author

astrale-sharp commented Apr 28, 2023 via email

Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

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

You haven't addressed my comments with quote syntax correctly. Also, could you fix CI (make sure every file has a license header)?

For both issues, have a look at how existing code does it 😉

@astrale-sharp
Copy link
Contributor Author

I wasn't totally sure what you meant about quote!, if the change still doesn't correspond to what you want, please provide an example or a more thorough explanation ! (but honestly i think I got it now)

@astrale-sharp
Copy link
Contributor Author

astrale-sharp commented Apr 30, 2023

forgot godot::builtin::
sorry about that
Edit :: removed the import of the prelude in test to make it very apparent when I forgot godot::builtin somewhere

Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

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

Thanks a lot, this is really great work! ❤️

Added some inline comments, mostly stylistic ones.

You seem to use a lot of mut when modifying token streams, and slowly build them via appending to a previous quote! expression. Maybe as a hint, you can use interpolations on quote! to repeat patterns, without needing a for loop and procedural element pushing/popping. This also works on iterators, so you can use #( #expr )* if expr is the result of iter().map() for example; you don't need an actual collection backing it.

Interpolations are a nice pattern similar to declarative macros, and having less mut variables in the code can make things a bit easier to understand.

godot-macros/src/derive_from_variant.rs Outdated Show resolved Hide resolved
godot-macros/src/derive_from_variant.rs Outdated Show resolved Hide resolved
godot-macros/src/derive_from_variant.rs Outdated Show resolved Hide resolved
godot-macros/src/derive_from_variant.rs Outdated Show resolved Hide resolved
godot-macros/src/derive_from_variant.rs Show resolved Hide resolved
godot-macros/src/util.rs Outdated Show resolved Hide resolved
itest/rust/src/derive_variant.rs Outdated Show resolved Hide resolved
itest/rust/src/derive_variant.rs Outdated Show resolved Hide resolved
itest/rust/src/derive_variant.rs Outdated Show resolved Hide resolved
itest/rust/src/derive_variant.rs Outdated Show resolved Hide resolved
@astrale-sharp astrale-sharp force-pushed the derive branch 4 times, most recently from 0939d62 to f8c59c5 Compare May 7, 2023 06:29
@astrale-sharp
Copy link
Contributor Author

Thank you very much for your comments/review/positivity, it's been a pleasure to contribute!

Time for another review (with one question pending) we should be close!

@astrale-sharp astrale-sharp requested a review from Bromeon May 7, 2023 06:30
Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the update!

A few comments left (mostly formatting-related), then should be good. A few suggestions from previous review you overlooked, I marked them again so they're not lost in this huge changeset 🙂


A side note, the struct MyClass { i: 33, b: true } can theoretically map to two dictionaries:

  • { "MyClass": { i: 77, b: true } } -- as done here
  • { i: 77, b: true }

And both have their use cases. It's definitely great to already have an initial implementation, so no need to change anything now! This could always be done later, or maybe even customizable.

godot-macros/src/derive_from_variant.rs Outdated Show resolved Hide resolved
godot-macros/src/derive_from_variant.rs Outdated Show resolved Hide resolved
godot-macros/src/derive_from_variant.rs Outdated Show resolved Hide resolved
godot-macros/src/derive_to_variant.rs Show resolved Hide resolved
godot-macros/src/derive_to_variant.rs Outdated Show resolved Hide resolved
godot-macros/src/derive_to_variant.rs Outdated Show resolved Hide resolved
godot-macros/src/util.rs Outdated Show resolved Hide resolved
itest/rust/src/derive_variant.rs Outdated Show resolved Hide resolved
itest/rust/src/derive_variant.rs Outdated Show resolved Hide resolved
@GodotRust
Copy link

API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-246

@astrale-sharp
Copy link
Contributor Author

Hi! I'm not sick anymore! ;)
Hopefully I didn't forget anything this time 👼

Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

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

Great to hear! And thanks for the update, it's mostly good now.
You missed a few smaller comments, I wrote them down again 🙂

Could you squash all comments to a single one?

godot-core/src/builtin/variant/variant_traits.rs Outdated Show resolved Hide resolved
godot-macros/src/derive_to_variant.rs Outdated Show resolved Hide resolved
godot-macros/src/derive_to_variant.rs Show resolved Hide resolved
@astrale-sharp astrale-sharp force-pushed the derive branch 2 times, most recently from a67a5d1 to e32998d Compare May 19, 2023 13:12
@astrale-sharp
Copy link
Contributor Author

Thanks for the quick review! Good to go?

@astrale-sharp
Copy link
Contributor Author

It looked good to go but more testing showed that it was relying on types being copy, I modified the test with !Copy types and modified the code_gen to use clone instead.

@Bromeon
Copy link
Member

Bromeon commented May 24, 2023

bors try

bors bot added a commit that referenced this pull request May 24, 2023
@Bromeon
Copy link
Member

Bromeon commented May 24, 2023

bors p=9
bors r+

@bors

This comment was marked as resolved.

@bors
Copy link
Contributor

bors bot commented May 24, 2023

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@bors bors bot merged commit 34f0ffe into godot-rust:master May 24, 2023
7 checks passed
@Bromeon
Copy link
Member

Bromeon commented May 24, 2023

Thanks a lot for this great addition! 🚀

@Bromeon Bromeon mentioned this pull request Jun 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: core Core components feature Adds functionality to the library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: FromVariant ToVariant derive proc macros
3 participants