This is a simplified version of my code:
fn get_widget_by_name<T: ContainerExt, K: WidgetExt>(widget_id: String, start: K) -> Option<K> {
if start.widget_name().to_string() == widget_id {
return start;
}
let bin_type: gtk::glib::Type =
gtk::prelude::ObjectExt::type_(>k::Window::new(gtk::WindowType::Toplevel))
.parent()
.unwrap();
let mut is_bin = bin == start.type_();
if !is_container {
let tmp = start.type_().parent();
if let Some(tmp) = tmp {
is_container = bin_type == tmp;
}
}
if is_container {
// I want to call .children() here, but
// I can't because WidgetExt doesn't
// implement it, and this function
// takes a WidgetExt.
}
...
}
In this case I know more than the compiler. The code checks to see if it’s a subclass of Bin, so I’m fine with it returning an Err
.
Obviously I could refactor my code so that the function only takes a ContainerExt, and works from there, but then what do I do with e.g. callbacks? Those return gtk::Widget
s, which do not implement ContainerExt
.
I looked at Any
and downcasting,b ut that only supports downcasting to a concrete type.
Is there an idiomatic way to fix this, or am I looking at this from the wrong angle?