bus: add_watch() can fail as there can only be one watch at a time

Return an Option<SourceId> because of that.
This commit is contained in:
Sebastian Dröge 2019-02-15 13:30:05 +02:00
parent e24efa7259
commit a5a016557f

View file

@ -111,35 +111,47 @@ impl Bus {
} }
} }
pub fn add_watch<F>(&self, func: F) -> SourceId pub fn add_watch<F>(&self, func: F) -> Option<SourceId>
where where
F: FnMut(&Bus, &Message) -> Continue + Send + 'static, F: FnMut(&Bus, &Message) -> Continue + Send + 'static,
{ {
unsafe { unsafe {
from_glib(ffi::gst_bus_add_watch_full( let res = ffi::gst_bus_add_watch_full(
self.to_glib_none().0, self.to_glib_none().0,
glib_ffi::G_PRIORITY_DEFAULT, glib_ffi::G_PRIORITY_DEFAULT,
Some(trampoline_watch::<F>), Some(trampoline_watch::<F>),
into_raw_watch(func), into_raw_watch(func),
Some(destroy_closure_watch::<F>), Some(destroy_closure_watch::<F>),
)) );
if res == 0 {
None
} else {
Some(from_glib(res))
}
} }
} }
pub fn add_watch_local<F>(&self, func: F) -> SourceId pub fn add_watch_local<F>(&self, func: F) -> Option<SourceId>
where where
F: FnMut(&Bus, &Message) -> Continue + 'static, F: FnMut(&Bus, &Message) -> Continue + 'static,
{ {
unsafe { unsafe {
assert!(glib::MainContext::ref_thread_default().is_owner()); assert!(glib::MainContext::ref_thread_default().is_owner());
from_glib(ffi::gst_bus_add_watch_full( let res = ffi::gst_bus_add_watch_full(
self.to_glib_none().0, self.to_glib_none().0,
glib_ffi::G_PRIORITY_DEFAULT, glib_ffi::G_PRIORITY_DEFAULT,
Some(trampoline_watch::<F>), Some(trampoline_watch::<F>),
into_raw_watch(func), into_raw_watch(func),
Some(destroy_closure_watch::<F>), Some(destroy_closure_watch::<F>),
)) );
if res == 0 {
None
} else {
Some(from_glib(res))
}
} }
} }