New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Downcast trait for Option<T> #497
Comments
Sounds useful, yes |
What makes it harder is that get_child is not a |
Is there something special about |
Nothing special about it, there are definitely other functions that could benefit from a generic alternative. Do you have any others in mind?
Yeah no, there's no way to detect/generate that automatically from gir |
|
Maybe is possible to specialize, something like: trait OptionCast { /* ... */ } // Similar to Cast trait, but returns the value or panics
impl OptionCast for Option<glib::Object> {
fn downcast<T: ObjectType>(self) -> T {
panic!("failed to cast to `{}`", T::static_type())
}
}
// Now
list_item.get_child().unwrap().downcast::<gtk::Label>().expect("failed to cast to `GtkLabel`");
// After
list_item.get_child().downcast::<gtk::Label>(); |
@bilelmoussaoui I don't think this needs to be on 0.1.0 |
Yeah, my idea was to also do it for get_item() and various other functions, but that's a lot of work manually. I'm thinking we would either want to have gir generate a |
@sdroege What do you think of something like this? without the panic part. I suppose it's similar to the ListModel::item case where you lose the semantics of the |
You could implement |
I am going to move this to gtk-rs-core as such trait implementation has to be done on glib-rs side |
Fixed in #843 |
A lot of the time I do something like this:
This gets to be really tedious and ugly. I propose gtk-rs should add a function equivalent to the above, something like:
The text was updated successfully, but these errors were encountered: