Move catch_panic_pad_function() into a generic place for all element subclasses

This commit is contained in:
Sebastian Dröge 2018-04-05 11:34:57 +03:00
parent 7bffce7a6f
commit 40f243dfc3
2 changed files with 27 additions and 15 deletions

View file

@ -259,21 +259,6 @@ impl ToggleRecord {
Box::new(imp)
}
fn catch_panic_pad_function<T, F: FnOnce(&Self, &Element) -> T, G: FnOnce() -> T>(
parent: &Option<gst::Object>,
fallback: G,
f: F,
) -> T {
let element = parent
.as_ref()
.cloned()
.unwrap()
.downcast::<Element>()
.unwrap();
let togglerecord = element.get_impl().downcast_ref::<ToggleRecord>().unwrap();
element.catch_panic(fallback, |element| f(togglerecord, element))
}
fn set_pad_functions(sinkpad: &gst::Pad, srcpad: &gst::Pad) {
sinkpad.set_chain_function(|pad, parent, buffer| {
ToggleRecord::catch_panic_pad_function(

View file

@ -53,6 +53,33 @@ pub trait ElementImpl<T: ElementBase>: ObjectImpl<T> + AnyImpl + Send + Sync + '
}
}
use std::any::Any;
pub trait ElementImplExt<T> {
fn catch_panic_pad_function<R, F: FnOnce(&Self, &T) -> R, G: FnOnce() -> R>(
parent: &Option<gst::Object>,
fallback: G,
f: F,
) -> R;
}
impl<S: ElementImpl<T>, T: ObjectType + glib::IsA<gst::Element> + glib::IsA<gst::Object>> ElementImplExt<T> for S {
fn catch_panic_pad_function<R, F: FnOnce(&Self, &T) -> R, G: FnOnce() -> R>(
parent: &Option<gst::Object>,
fallback: G,
f: F,
) -> R {
let element = parent
.as_ref()
.cloned()
.unwrap()
.downcast::<T>()
.unwrap();
let imp = Any::downcast_ref::<Self>(element.get_impl()).unwrap();
element.catch_panic(fallback, |element| f(imp, element))
}
}
any_impl!(ElementBase, ElementImpl);
pub unsafe trait ElementBase: IsA<gst::Element> + ObjectType {