0

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.

2
  • Create a trait with your needed methods, implement it for all types you want to accept, use macros to avoid boilerplate. Commented Oct 1, 2023 at 10:43
  • OK I see thc. Just want to know if there is an easy way around this, but apparently there isn't. Anyway thx! Commented Oct 1, 2023 at 10:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.