gtk4-rs
gtk4-rs copied to clipboard
[BUG] `gdk::Paintable.snapshot()` doesn't accept `gtk::Snapshot`
Bug description
The gdk::Paintable.snapshot() method doesn't accept a reference to a gtk::Snapshot. The compiler complains with this:
error[E0308]: mismatched types
--> src/main.rs:74:40
|
74 | paintable.snapshot(&snapshot, width.into(), height.into());
| ^^^^^^^^^ expected struct `gtk4::gdk4::Snapshot`, found struct `gtk4::Snapshot`
|
= note: expected reference `>k4::gdk4::Snapshot`
found reference `>k4::Snapshot`
For more information about this error, try `rustc --explain E0308`.
It expects a gdk::Snapshot, which gtk::Snapshot is according to the docs.
I call it with this code:
let snapshot = gtk::Snapshot::new();
paintable.snapshot(&snapshot, width, height);
Replacing &snapshot by snapshot.upcast_ref() gets rid of the error.
From my side, the two things that are desirable in practice are
- Being able to call the gtk::Snapshot methods on the gdk::Snapshot provided by the
snapshotvfunc on widgets and paintables - Be able to pass gtk::Snapshots to the snapshot vfuncs
The easiest fix I can think of is to make all those methods take a AsRef<gdk::Snapshot> & implement that trait on both :-)
An alternative would be moving the methods from gtk::Snapshot to a trait and implement that trait for gdk::Snapshot that though won't allow passing a gtk::Snapshot to a method that expects a gdk::Snapshot so we kind of need both.
An alternative would be moving the methods from
gtk::Snapshotto a trait and implement that trait forgdk::Snapshotthat though won't allow passing agtk::Snapshotto a method that expects agdk::Snapshotso we kind of need both.
What I don't like about this approach is that it requires manually implementing all the methods of gtk::Snapshot... Because gir only generates a AsRef relation when it is a fundamental type and uses IsA if it is an object(which is the case here).
I wonder if it would make sense to have some kind of config for setting that you want an AsRef instead of IsA somewhere in gir.
Be able to pass gtk::Snapshots to the snapshot vfuncs
That's easily fixed by marking gdk::Snapshot not as final type and have the trait be generated.
Being able to call the gtk::Snapshot methods on the gdk::Snapshot provided by the
snapshotvfunc on widgets and paintables
That looks like a problem. gtk::Snapshot is actually a different type (a subclass of gdk::Snapshot) and it has some additional internal state. I don't think you can call the gtk::Snapshot API on every gdk::Snapshot, in case others than gtk::Snapshot exist at some point as non-abstract types.