How to create a generic function that supports multiple Widget types in Rust / GTK-rs?

39 views Asked by At

In nutshell, I wanna create this func (not valid Rust):

fn foo<T: IsA<CheckButton> | T: IsA<ToggleButton>>(...) {
    ...
    widget.is_active() // where widget is either a Checkbutton or a ToggleButton GTK4 widget
}

But this doesn't work in Rust.

I know I can do this:

fn foo<T: IsA<Object> + IsA<Widget>>(...) {
    ...
    widget.is_active() // Compiler complains that this method doesn't exist on Widget..
}

However this doesn't work either, because both ToggleButton and CheckButton has its own implementation of is_active() and they don't inherit it from Widget.

Is it even possible to make a generic function in such a case?

Btw I'm using GTK4 and GTK4-rs.

0

There are 0 answers