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

examples: Add example for custom class structs and virtual methods #1378

Merged
merged 1 commit into from
May 11, 2024

Conversation

felinira
Copy link
Contributor

Adds a very detailed example of virtual methods and interfaces. We already had one in gtk4, but not here. I also couldn't find any example for properly chaining up interface vfuncs.

The example is a bit elaborate, but I am convinced that every class here has some merit. It was for example unexpected to me that the interface had to be re-declared in the subclass a second time to override its vfunc, as this is not required for classes. I can however split it up if that is desired.

Some questions that came up:

  • Should some of these Ext traits be sealed? They have a bound that is equal to their implementation, so I'm not entirely sure what that would accomplish.
  • All examples of subclassing I found put the class struct out of the implementation module. The few interface implementations I found in the wild don't do that, and instead put them in a separate module, calling the class struct the same as the type. What should be best practise here?

@sdroege
Copy link
Member

sdroege commented Apr 29, 2024

Adds a very detailed example of virtual methods and interfaces. We already had one in gtk4, but not here. I also couldn't find any example for properly chaining up interface vfuncs.

https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/tree/main/net/rtp/src/basedepay, https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/tree/main/net/rtp/src/baseaudiopay and https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/tree/main/net/rtp/src/basepay might also be of interest to you then :)

@sdroege sdroege self-requested a review April 29, 2024 18:55
@sdroege
Copy link
Member

sdroege commented Apr 30, 2024

  • Should some of these Ext traits be sealed? They have a bound that is equal to their implementation, so I'm not entirely sure what that would accomplish.

Nothing, they shouldn't be sealed :) Do we have any left somewhere that are sealed in this situation? If so we should fix that. I thought I did that a while ago already.

  • All examples of subclassing I found put the class struct out of the implementation module. The few interface implementations I found in the wild don't do that, and instead put them in a separate module, calling the class struct the same as the type. What should be best practise here?

The struct should be outside the implementation module (unless it's a final type, etc). It's public API, even if you don't use it from Rust directly (usually).

Copy link
Member

@sdroege sdroege left a comment

Choose a reason for hiding this comment

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

Nice example, and much better than https://github.com/sdroege/gobject-example-rs

Now only need to add C API stuff here and we can retire that one

@GuillaumeGomez
Copy link
Member

Awesome, thanks!

@sdroege sdroege requested a review from pbor April 30, 2024 12:15
@felinira felinira force-pushed the fina/virtual-methods-example branch 2 times, most recently from 304e98b to 580270e Compare May 2, 2024 21:11
@sdroege
Copy link
Member

sdroege commented May 9, 2024

@felinira This needs to be rebased around #1384 now, and I guess then we can get it in as a first version and improve on it later as needed?

@felinira felinira force-pushed the fina/virtual-methods-example branch 2 times, most recently from 26f4219 to fc5a0b4 Compare May 10, 2024 19:49
@felinira
Copy link
Contributor Author

@sdroege Done. Now that the ffi boundary is more clearly defined I took the liberty of moving the class structs into the ffi module.

@felinira felinira force-pushed the fina/virtual-methods-example branch from fc5a0b4 to 551cb54 Compare May 10, 2024 19:50
@sdroege sdroege merged commit c947074 into gtk-rs:master May 11, 2024
47 of 48 checks passed
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.

3 participants