-
-
Notifications
You must be signed in to change notification settings - Fork 107
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
Generate impl
blocks for associated enum functions
#991
Conversation
The exact same code would also apply to flags types, or not? Can that be unified so (mostly) the same code can cover both? Also if you could submit all the cleanup commits as a separate PR that would speed up reviewing here. The cleanups all look fine from what I saw so far and could be merged more or less immediately :) |
Can you also check what (if any) effects this has on https://github.com/gtk-rs/gtk-rs and submit a WIP PR? |
I guess you mean
Pushed as #986, including some commits that change generated output (ie.
As mentioned in the edit above I or someone else will have to go through gtk and remove the same handwritten implementations that clash with generated functions, while also validating semantics of generated code (strings...). |
Yeah, |
This needs to be rebased now |
7b7bec9
to
c411a37
Compare
That's OK for now, just submit a PR that does not remove the handwritten functions so we can easily compare what is generated there too :) |
c411a37
to
cdb578b
Compare
@sdroege Removed them anyway because it was only one class, as well as fixing multiple doc issues that appeared when testing with There's a temp commit here to make sure the CI builds on the fixed gtk-rs branch. Once this gets closer I guess we'll merge gtk-rs, bump the commit off and make sure it succeeds one final time before merging? |
@sdroege Pushed a simple yet unsustainable version of a safer, static The benefit of having The only problem I see right now is formatting in comments and macros ( Further advantages:
Conversion is pretty easy, too: copy an existing block of generated code (instead of tearing down |
10b8f8b
to
37af52f
Compare
What's that commit and why? :) |
Let's indeed do that as a separate PR afterwards. I agree that the string manipulations without quote are annoying. Last time I tried moving to quote the big problem was that it was rather annoying to a) do comments, b) format code in a reasonable way (e.g. quote didn't output empty lines between any items IIRC), c) even rustfmt was not converting the code into something reasonable looking. If that can be solved I'm fine with moving to quote. But as said, different PR. Not too many things at once :) |
Looks ok to me otherwise. So
Doesn't seem like too much missing here :) |
That commit switched the CI over to use my enum functions gtk-rs branch so that we can see the whole thing working and make sure the CI succeeds :)
I'm glad to hear quote gets a second chance! The only issues with Guess it only makes sense to, for a first approach, convert (or implement new) tiny bits of
Of course, pushing it here was merely to showcase :D |
37af52f
to
165999f
Compare
I think that can make sense. AFAIU you can call rustfmt also as a library so that might be worth looking into.
Agreed. But now let's first try to get this PR done and not put too much other noise in here :) |
0ca3086
to
82ca289
Compare
cc20ba3
to
b2ce127
Compare
@GuillaumeGomez On you now, this one is "1 change requested" from you :) |
Actually pending a decision on https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/630#note_719829 from my side |
Analysis was copied from enums which adds an std::fmt when generate_display_trait is true, but the codegen for it was missing. Instead of removing the import, provide a simple Display trait that forwards to bitflags' Debug implementation.
When get_name or to_string is available and adheres to the right conditions a Display impl leveraging this C implementation is generated. This will clash with a pure Rust impl if generate_display_trait is true which should be disabled to prefer the C function call instead.
Complements: 5ce31f2 ("codegen: Add version condition on special function traits")
1ef3cbe
to
85c8c21
Compare
85c8c21
to
2915bde
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still looks good to me. GStreamer generated code is ready too
No more |
Leverage existing function generation for class/record to generate associated functions on enumerations as well, instead of having to write these by hand 🎉. The MR for
gstreamer-rs
can be found here. These changes came up during https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/581.This relies on functions being associated with the
<enumeration>
in.gir
files as opposed to being free (same story with class/record), see https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/805 for an example where this was not the case.Note that we have decided against turning the first argument from
<parameter>
into<instance-parameter>
in Gir files (to make these functions behave likefn my_func(&self)
instead offn my_func(my_enum: Self)
). This is instead overridden during analysis, it's a bit ugly so perhaps we need to revisit this discussion. It all depends on other tooling, and how many other languages (potentially) support functions on enum instances like Rust.Related PRs/MRs
TODO
to_string
seems quite a large task still:fn to_string(self)
(ie. by naming it consistentlyto_str
across the board)use glib::GString
inwebrtc/enums
(due to&'static str
replacement)Err
in favour ofNone
.Removed from this PR, separate PR comes when rebasing doesn't conflict with these changes anymore 😉
AutogenerateNot worth it: many different types use this function, all with their own semantics.from_string
, which is pretty straightforwardstd::fmt
in flags whengenerate_display_trait
is true but nothing hasto_string
(only problematic in gtk)std::fmt
whengenerate_display_trait
is true, without any matching codegen. A sensible Display trait is now emitted in that case.gst_video_chroma_*
functions togst_video_chroma_site_*
for autogeneration.fn from_*(xxx) -> EnumType
), turn that into aResult<..., BoolError>
;