pub trait CastNone: Sized {
type Inner;
// Required methods
fn and_downcast<T: ObjectType>(self) -> Option<T>
where Self::Inner: CanDowncast<T>;
fn and_downcast_ref<T: ObjectType>(&self) -> Option<&T>
where Self::Inner: CanDowncast<T>;
fn and_upcast<T: ObjectType>(self) -> Option<T>
where Self::Inner: IsA<T>;
fn and_upcast_ref<T: ObjectType>(&self) -> Option<&T>
where Self::Inner: IsA<T>;
fn and_dynamic_cast<T: ObjectType>(self) -> Result<T, Self>;
fn and_dynamic_cast_ref<T: ObjectType>(&self) -> Option<&T>;
}
Expand description
Convenience trait mirroring Cast
, implemented on Option<Object>
types.
Warning
Inveitably this trait will discard informations about a downcast failure:
you don’t know if the object was not of the expected type, or if it was None
.
If you need to handle the downcast error, use Cast
over a glib::Object
.
Example
ⓘ
let widget: Option<Widget> = list_item.child();
// Without using `CastNone`
let label = widget.unwrap().downcast::<gtk::Label>().unwrap();
// Using `CastNone` we can avoid the first `unwrap()` call
let label = widget.and_downcast::<gtk::Label>().unwrap();