From 3e3c5205dbde6e53a0d3ecc69716b23447737de8 Mon Sep 17 00:00:00 2001 From: Tony Jinwoo Ahn Date: Sun, 15 Dec 2019 08:36:56 +0000 Subject: [PATCH] gstreamer-video/video_info: Change functions from returning Option to Result Partial work for: https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/issues/216 --- examples/src/bin/glupload.rs | 4 ++-- gstreamer-video/src/video_info.rs | 28 ++++++++++++++++------------ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/examples/src/bin/glupload.rs b/examples/src/bin/glupload.rs index 5038ce238..e139d1dc8 100644 --- a/examples/src/bin/glupload.rs +++ b/examples/src/bin/glupload.rs @@ -499,7 +499,7 @@ impl App { let _info = sample .get_caps() - .and_then(|caps| gst_video::VideoInfo::from_caps(caps)) + .and_then(|caps| gst_video::VideoInfo::from_caps(caps).ok()) .ok_or_else(|| { gst_element_error!( appsink, @@ -652,7 +652,7 @@ fn main_loop(mut app: App) -> Result { } impl<'a> VideoInfoBuilder<'a> { - pub fn build(self) -> Option { + pub fn build(self) -> Result { unsafe { let mut info = mem::MaybeUninit::uninit(); @@ -306,13 +306,13 @@ impl<'a> VideoInfoBuilder<'a> { }; if !res { - return None; + return Err(glib_bool_error!("Failed to build VideoInfo")); } let mut info = info.assume_init(); if info.finfo.is_null() || info.width <= 0 || info.height <= 0 { - return None; + return Err(glib_bool_error!("Failed to build VideoInfo")); } if let Some(flags) = self.flags { @@ -347,7 +347,7 @@ impl<'a> VideoInfoBuilder<'a> { if let Some(offset) = self.offset { if offset.len() != ((*info.finfo).n_planes as usize) { - return None; + return Err(glib_bool_error!("Failed to build VideoInfo")); } let n_planes = (*info.finfo).n_planes as usize; @@ -356,7 +356,7 @@ impl<'a> VideoInfoBuilder<'a> { if let Some(stride) = self.stride { if stride.len() != ((*info.finfo).n_planes as usize) { - return None; + return Err(glib_bool_error!("Failed to build VideoInfo")); } let n_planes = (*info.finfo).n_planes as usize; @@ -381,7 +381,7 @@ impl<'a> VideoInfoBuilder<'a> { } } - Some(VideoInfo(info)) + Ok(VideoInfo(info)) } } @@ -526,7 +526,7 @@ impl VideoInfo { } } - pub fn from_caps(caps: &gst::CapsRef) -> Option { + pub fn from_caps(caps: &gst::CapsRef) -> Result { skip_assert_initialized!(); unsafe { @@ -535,18 +535,22 @@ impl VideoInfo { info.as_mut_ptr(), caps.as_ptr(), )) { - Some(VideoInfo(info.assume_init())) + Ok(VideoInfo(info.assume_init())) } else { - None + Err(glib_bool_error!("Failed to create VideoInfo from caps")) } } } - pub fn to_caps(&self) -> Option { + pub fn to_caps(&self) -> Result { unsafe { - from_glib_full(gst_video_sys::gst_video_info_to_caps( + let result = from_glib_full(gst_video_sys::gst_video_info_to_caps( &self.0 as *const _ as *mut _, - )) + )); + match result { + Some(c) => Ok(c), + None => Err(glib_bool_error!("Failed to create caps from VideoInfo")), + } } }