From 139c9be9588e6e86887090e3b94be3448d49fd2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Mon, 24 Apr 2017 10:13:32 +0100 Subject: [PATCH] Minor cleanup --- gst-plugin/src/caps.rs | 12 ++++++++++-- gst-plugin/src/miniobject.rs | 2 ++ gst-plugin/src/structure.rs | 17 ++++++----------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/gst-plugin/src/caps.rs b/gst-plugin/src/caps.rs index f5fb9163..61e373a4 100644 --- a/gst-plugin/src/caps.rs +++ b/gst-plugin/src/caps.rs @@ -84,14 +84,22 @@ impl Caps { pub fn get_structure(&self, idx: u32) -> Option<&Structure> { unsafe { let structure = gst::gst_caps_get_structure(self.as_ptr(), idx); - Structure::from_borrowed_ptr(structure as *const gst::GstStructure) + if structure.is_null() { + return None; + } + + Some(Structure::from_borrowed_ptr(structure as *const gst::GstStructure)) } } pub fn get_mut_structure(&mut self, idx: u32) -> Option<&mut Structure> { unsafe { let structure = gst::gst_caps_get_structure(self.as_ptr(), idx); - Structure::from_borrowed_mut_ptr(structure as *mut gst::GstStructure) + if structure.is_null() { + return None; + } + + Some(Structure::from_borrowed_mut_ptr(structure as *mut gst::GstStructure)) } } diff --git a/gst-plugin/src/miniobject.rs b/gst-plugin/src/miniobject.rs index cdc2aa63..d1635360 100644 --- a/gst-plugin/src/miniobject.rs +++ b/gst-plugin/src/miniobject.rs @@ -146,10 +146,12 @@ pub unsafe trait MiniObject } unsafe fn from_ptr<'a>(ptr: *const Self::PtrType) -> &'a Self { + assert!(!ptr.is_null()); &*(ptr as *const Self) } unsafe fn from_mut_ptr<'a>(ptr: *mut Self::PtrType) -> &'a mut Self { + assert!(!ptr.is_null()); &mut *(ptr as *mut Self) } } diff --git a/gst-plugin/src/structure.rs b/gst-plugin/src/structure.rs index 64702e7c..36cdc3dc 100644 --- a/gst-plugin/src/structure.rs +++ b/gst-plugin/src/structure.rs @@ -141,21 +141,16 @@ impl ToOwned for Structure { pub struct Structure(gst::GstStructure); impl Structure { - pub unsafe fn from_borrowed_ptr<'a>(ptr: *const gst::GstStructure) -> Option<&'a Structure> { - if ptr.is_null() { - return None; - } + pub unsafe fn from_borrowed_ptr<'a>(ptr: *const gst::GstStructure) -> &'a Structure { + assert!(!ptr.is_null()); - Some(&*(ptr as *mut Structure)) + &*(ptr as *mut Structure) } - pub unsafe fn from_borrowed_mut_ptr<'a>(ptr: *mut gst::GstStructure) - -> Option<&'a mut Structure> { - if ptr.is_null() { - return None; - } + pub unsafe fn from_borrowed_mut_ptr<'a>(ptr: *mut gst::GstStructure) -> &'a mut Structure { + assert!(!ptr.is_null()); - Some(&mut *(ptr as *mut Structure)) + &mut *(ptr as *mut Structure) } pub fn to_string(&self) -> String {