diff --git a/gstreamer/src/buffer_pool.rs b/gstreamer/src/buffer_pool.rs index aa4072943..0cb71a9e5 100644 --- a/gstreamer/src/buffer_pool.rs +++ b/gstreamer/src/buffer_pool.rs @@ -234,7 +234,7 @@ pub trait BufferPoolExtManual { fn acquire_buffer<'a, P: Into>>( &self, params: P, - ) -> Result<::Buffer, ::FlowReturn>; + ) -> Result<::Buffer, ::FlowError>; fn release_buffer(&self, buffer: ::Buffer); } @@ -267,7 +267,7 @@ impl> BufferPoolExtManual for O { fn acquire_buffer<'a, P: Into>>( &self, params: P, - ) -> Result<::Buffer, ::FlowReturn> { + ) -> Result<::Buffer, ::FlowError> { let params = params.into(); let params_ptr = match params { Some(params) => ¶ms.0 as *const _ as *mut _, @@ -276,17 +276,13 @@ impl> BufferPoolExtManual for O { unsafe { let mut buffer = ptr::null_mut(); - let ret = from_glib(ffi::gst_buffer_pool_acquire_buffer( + let ret: ::FlowReturn = from_glib(ffi::gst_buffer_pool_acquire_buffer( self.to_glib_none().0, &mut buffer, params_ptr, )); - if ret == ::FlowReturn::Ok { - Ok(from_glib_full(buffer)) - } else { - Err(ret) - } + ret.into_result_value(|| from_glib_full(buffer)) } } diff --git a/gstreamer/src/enums.rs b/gstreamer/src/enums.rs index 1d84cbd2a..0c27ac4e8 100644 --- a/gstreamer/src/enums.rs +++ b/gstreamer/src/enums.rs @@ -95,6 +95,13 @@ impl FlowReturn { } } + pub fn into_result_value T>(self, func: F) -> Result { + match self.into_result() { + Ok(_) => Ok(func()), + Err(err) => Err(err), + } + } + pub fn from_error(v: FlowError) -> Self { match v { FlowError::NotLinked => FlowReturn::NotLinked, diff --git a/gstreamer/src/pad.rs b/gstreamer/src/pad.rs index 47e0567aa..b0cb61be9 100644 --- a/gstreamer/src/pad.rs +++ b/gstreamer/src/pad.rs @@ -10,6 +10,7 @@ use miniobject::MiniObject; use Buffer; use BufferList; use Event; +use FlowError; use FlowReturn; use Format; use FormattedValue; @@ -115,8 +116,8 @@ pub trait PadExtManual { fn chain_list(&self, list: BufferList) -> FlowReturn; fn push_list(&self, list: BufferList) -> FlowReturn; - fn pull_range(&self, offset: u64, size: u32) -> Result; - fn get_range(&self, offset: u64, size: u32) -> Result; + fn pull_range(&self, offset: u64, size: u32) -> Result; + fn get_range(&self, offset: u64, size: u32) -> Result; fn peer_query(&self, query: &mut QueryRef) -> bool; fn query(&self, query: &mut QueryRef) -> bool; @@ -170,7 +171,7 @@ pub trait PadExtManual { fn set_getrange_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, u64, u32) -> Result<::Buffer, ::FlowReturn> + F: Fn(&Pad, &Option<::Object>, u64, u32) -> Result<::Buffer, ::FlowError> + Send + Sync + 'static; @@ -290,37 +291,29 @@ impl> PadExtManual for O { } } - fn get_range(&self, offset: u64, size: u32) -> Result { + fn get_range(&self, offset: u64, size: u32) -> Result { unsafe { let mut buffer = ptr::null_mut(); - let ret = from_glib(ffi::gst_pad_get_range( + let ret: FlowReturn = from_glib(ffi::gst_pad_get_range( self.to_glib_none().0, offset, size, &mut buffer, )); - if ret == FlowReturn::Ok { - Ok(from_glib_full(buffer)) - } else { - Err(ret) - } + ret.into_result_value(|| from_glib_full(buffer)) } } - fn pull_range(&self, offset: u64, size: u32) -> Result { + fn pull_range(&self, offset: u64, size: u32) -> Result { unsafe { let mut buffer = ptr::null_mut(); - let ret = from_glib(ffi::gst_pad_pull_range( + let ret: FlowReturn = from_glib(ffi::gst_pad_pull_range( self.to_glib_none().0, offset, size, &mut buffer, )); - if ret == FlowReturn::Ok { - Ok(from_glib_full(buffer)) - } else { - Err(ret) - } + ret.into_result_value(|| from_glib_full(buffer)) } } @@ -544,7 +537,7 @@ impl> PadExtManual for O { fn set_getrange_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, u64, u32) -> Result<::Buffer, ::FlowReturn> + F: Fn(&Pad, &Option<::Object>, u64, u32) -> Result<::Buffer, ::FlowError> + Send + Sync + 'static, @@ -552,7 +545,7 @@ impl> PadExtManual for O { unsafe { #[cfg_attr(feature = "cargo-clippy", allow(type_complexity))] let func_box: Box< - Fn(&Pad, &Option<::Object>, u64, u32) -> Result<::Buffer, ::FlowReturn> + Fn(&Pad, &Option<::Object>, u64, u32) -> Result<::Buffer, ::FlowError> + Send + Sync + 'static, @@ -1109,7 +1102,7 @@ unsafe extern "C" fn trampoline_getrange_function( buffer: *mut *mut ffi::GstBuffer, ) -> ffi::GstFlowReturn { #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let func: &&(Fn(&Pad, &Option<::Object>, u64, u32) -> Result<::Buffer, ::FlowReturn> + let func: &&(Fn(&Pad, &Option<::Object>, u64, u32) -> Result<::Buffer, ::FlowError> + Send + Sync + 'static) = transmute((*pad).getrangedata); @@ -1124,7 +1117,7 @@ unsafe extern "C" fn trampoline_getrange_function( *buffer = new_buffer.into_ptr(); ::FlowReturn::Ok.to_glib() } - Err(ret) => ret.to_glib(), + Err(ret) => ::FlowReturn::from_error(ret).to_glib(), } } diff --git a/gstreamer/src/proxy_pad.rs b/gstreamer/src/proxy_pad.rs index b949ebff3..6edb51efc 100644 --- a/gstreamer/src/proxy_pad.rs +++ b/gstreamer/src/proxy_pad.rs @@ -9,6 +9,7 @@ use std::ptr; use Buffer; use BufferList; +use FlowError; use FlowReturn; use Object; use Pad; @@ -59,22 +60,18 @@ impl ProxyPad { parent: &Q, offset: u64, size: u32, - ) -> Result { + ) -> Result { skip_assert_initialized!(); unsafe { let mut buffer = ptr::null_mut(); - let ret = from_glib(ffi::gst_proxy_pad_getrange_default( + let ret: FlowReturn = from_glib(ffi::gst_proxy_pad_getrange_default( pad.to_glib_none().0 as *mut ffi::GstPad, parent.to_glib_none().0, offset, size, &mut buffer, )); - if ret == FlowReturn::Ok { - Ok(from_glib_full(buffer)) - } else { - Err(ret) - } + ret.into_result_value(|| from_glib_full(buffer)) } }