gtk-rs-core icon indicating copy to clipboard operation
gtk-rs-core copied to clipboard

examples: Add example for custom class structs and virtual methods

Open felinira opened this issue 9 months ago • 4 comments

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?

felinira avatar Apr 29 '24 18:04 felinira

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 avatar Apr 29 '24 18:04 sdroege

  • 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).

sdroege avatar Apr 30 '24 07:04 sdroege

Awesome, thanks!

GuillaumeGomez avatar Apr 30 '24 11:04 GuillaumeGomez

@felinira This needs to be rebased around https://github.com/gtk-rs/gtk-rs-core/pull/1384 now, and I guess then we can get it in as a first version and improve on it later as needed?

sdroege avatar May 09 '24 05:05 sdroege

@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 avatar May 10 '24 19:05 felinira