gtk4-rs icon indicating copy to clipboard operation
gtk4-rs copied to clipboard

[BUG] `gdk::Paintable.snapshot()` doesn't accept `gtk::Snapshot`

Open rmnvgr opened this issue 3 years ago • 5 comments

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 `&gtk4::gdk4::Snapshot`
              found reference `&gtk4::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.

rmnvgr avatar Jan 07 '22 22:01 rmnvgr

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 snapshot vfunc on widgets and paintables
  • Be able to pass gtk::Snapshots to the snapshot vfuncs

A6GibKm avatar Jan 07 '22 22:01 A6GibKm

The easiest fix I can think of is to make all those methods take a AsRef<gdk::Snapshot> & implement that trait on both :-)

bilelmoussaoui avatar Jan 07 '22 22:01 bilelmoussaoui

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.

bilelmoussaoui avatar Jan 07 '22 22:01 bilelmoussaoui

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.

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.

bilelmoussaoui avatar Jan 08 '22 08:01 bilelmoussaoui

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 snapshot vfunc 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.

sdroege avatar Jan 08 '22 09:01 sdroege