gtk-rs / gtk-rs-core

Rust bindings for GNOME libraries
https://gtk-rs.org/gtk-rs-core
MIT License
272 stars 104 forks source link

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

Closed felinira closed 1 month ago

felinira commented 2 months ago

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:

sdroege commented 2 months ago

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 commented 2 months ago
  • 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).

GuillaumeGomez commented 2 months ago

Awesome, thanks!

sdroege commented 2 months ago

@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?

felinira commented 1 month ago

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