diff --git a/gstreamer/src/iterator.rs b/gstreamer/src/iterator.rs index cb584f43a..c55feb26f 100644 --- a/gstreamer/src/iterator.rs +++ b/gstreamer/src/iterator.rs @@ -50,7 +50,7 @@ impl Error for IteratorError { // Implemented manually so that we can use generics for the item pub struct Iterator { - iter: *mut ffi::GstIterator, + iter: ptr::NonNull, borrowed: bool, phantom: PhantomData, } @@ -492,7 +492,7 @@ impl Drop for Iterator { fn drop(&mut self) { if !self.borrowed { unsafe { - ffi::gst_iterator_free(self.iter); + ffi::gst_iterator_free(self.iter.as_ptr()); } } } @@ -545,7 +545,7 @@ impl<'a, T: 'static> glib::translate::ToGlibPtr<'a, *const ffi::GstIterator> for type Storage = &'a Iterator; fn to_glib_none(&'a self) -> glib::translate::Stash<'a, *const ffi::GstIterator, Self> { - glib::translate::Stash(self.iter, self) + glib::translate::Stash(self.iter.as_ptr(), self) } fn to_glib_full(&self) -> *const ffi::GstIterator { @@ -561,7 +561,7 @@ impl<'a, T: 'static> glib::translate::ToGlibPtrMut<'a, *mut ffi::GstIterator> fo fn to_glib_none_mut( &'a mut self, ) -> glib::translate::StashMut<'a, *mut ffi::GstIterator, Self> { - glib::translate::StashMut(self.iter, self) + glib::translate::StashMut(self.iter.as_ptr(), self) } } @@ -593,12 +593,13 @@ impl glib::translate::FromGlibPtrNone<*mut ffi::GstIterator> for impl glib::translate::FromGlibPtrBorrow<*mut ffi::GstIterator> for Iterator { #[inline] unsafe fn from_glib_borrow(ptr: *mut ffi::GstIterator) -> Self { + assert!(!ptr.is_null()); assert_ne!( gobject_ffi::g_type_is_a((*ptr).type_, T::static_type().to_glib()), glib_ffi::GFALSE ); Self { - iter: ptr, + iter: ptr::NonNull::new_unchecked(ptr), borrowed: true, phantom: PhantomData, } @@ -609,12 +610,13 @@ impl glib::translate::FromGlibPtrBorrow<*mut ffi::GstIterator> fo impl glib::translate::FromGlibPtrFull<*mut ffi::GstIterator> for Iterator { #[inline] unsafe fn from_glib_full(ptr: *mut ffi::GstIterator) -> Self { + assert!(!ptr.is_null()); assert_ne!( gobject_ffi::g_type_is_a((*ptr).type_, T::static_type().to_glib()), glib_ffi::GFALSE ); Self { - iter: ptr, + iter: ptr::NonNull::new_unchecked(ptr), borrowed: false, phantom: PhantomData, } diff --git a/gstreamer/src/log.rs b/gstreamer/src/log.rs index 502d7f7c4..13e2b3da5 100644 --- a/gstreamer/src/log.rs +++ b/gstreamer/src/log.rs @@ -19,7 +19,7 @@ use glib::IsA; use glib::translate::{from_glib, ToGlib, ToGlibPtr}; #[derive(PartialEq, Eq, Clone, Copy)] -pub struct DebugCategory(*mut ffi::GstDebugCategory); +pub struct DebugCategory(ptr::NonNull); impl DebugCategory { pub fn new<'a, P: Into>>( @@ -38,11 +38,13 @@ impl DebugCategory { // Gets the category if it exists already unsafe { - DebugCategory(_gst_debug_category_new( + let ptr = _gst_debug_category_new( name.to_glib_none().0, color.to_glib(), description.to_glib_none().0, - )) + ); + assert!(!ptr.is_null()); + DebugCategory(ptr::NonNull::new_unchecked(ptr)) } } @@ -57,34 +59,34 @@ impl DebugCategory { if cat.is_null() { None } else { - Some(DebugCategory(cat)) + Some(DebugCategory(ptr::NonNull::new_unchecked(cat))) } } } pub fn get_threshold(&self) -> ::DebugLevel { - from_glib(unsafe { ffi::gst_debug_category_get_threshold(self.0) }) + from_glib(unsafe { ffi::gst_debug_category_get_threshold(self.0.as_ptr()) }) } pub fn set_threshold(&self, threshold: ::DebugLevel) { - unsafe { ffi::gst_debug_category_set_threshold(self.0, threshold.to_glib()) } + unsafe { ffi::gst_debug_category_set_threshold(self.0.as_ptr(), threshold.to_glib()) } } pub fn reset_threshold(&self) { - unsafe { ffi::gst_debug_category_reset_threshold(self.0) } + unsafe { ffi::gst_debug_category_reset_threshold(self.0.as_ptr()) } } pub fn get_color(&self) -> ::DebugColorFlags { unsafe { from_glib(mem::transmute::( - ffi::gst_debug_category_get_color(self.0), + ffi::gst_debug_category_get_color(self.0.as_ptr()), )) } } pub fn get_name(&self) -> &str { unsafe { - CStr::from_ptr(ffi::gst_debug_category_get_name(self.0)) + CStr::from_ptr(ffi::gst_debug_category_get_name(self.0.as_ptr())) .to_str() .unwrap() } @@ -92,7 +94,7 @@ impl DebugCategory { pub fn get_description(&self) -> Option<&str> { unsafe { - let ptr = ffi::gst_debug_category_get_name(self.0); + let ptr = ffi::gst_debug_category_get_name(self.0.as_ptr()); if ptr.is_null() { None @@ -113,7 +115,7 @@ impl DebugCategory { args: fmt::Arguments, ) { unsafe { - if level.to_glib() as i32 > (*self.0).threshold { + if level.to_glib() as i32 > self.0.as_ref().threshold { return; } } @@ -125,7 +127,7 @@ impl DebugCategory { unsafe { ffi::gst_debug_log( - self.0, + self.0.as_ptr(), level.to_glib(), file.to_glib_none().0, module.to_glib_none().0, diff --git a/gstreamer/src/miniobject.rs b/gstreamer/src/miniobject.rs index 90954588e..5f933547e 100644 --- a/gstreamer/src/miniobject.rs +++ b/gstreamer/src/miniobject.rs @@ -22,7 +22,7 @@ use glib::translate::{c_ptr_array_len, from_glib, from_glib_full, from_glib_none use glib; pub struct GstRc { - obj: *mut T, + obj: ptr::NonNull, borrowed: bool, phantom: PhantomData, } @@ -34,7 +34,7 @@ impl GstRc { ffi::gst_mini_object_ref(ptr as *mut ffi::GstMiniObject); GstRc { - obj: T::from_mut_ptr(ptr as *mut T::GstType) as *mut T, + obj: ptr::NonNull::new_unchecked(T::from_mut_ptr(ptr as *mut T::GstType) as *mut T), borrowed: false, phantom: PhantomData, } @@ -44,7 +44,7 @@ impl GstRc { assert!(!ptr.is_null()); GstRc { - obj: T::from_mut_ptr(ptr as *mut T::GstType) as *mut T, + obj: ptr::NonNull::new_unchecked(T::from_mut_ptr(ptr as *mut T::GstType) as *mut T), borrowed: false, phantom: PhantomData, } @@ -54,7 +54,7 @@ impl GstRc { assert!(!ptr.is_null()); GstRc { - obj: T::from_mut_ptr(ptr as *mut T::GstType) as *mut T, + obj: ptr::NonNull::new_unchecked(T::from_mut_ptr(ptr as *mut T::GstType) as *mut T), borrowed: true, phantom: PhantomData, } @@ -63,22 +63,23 @@ impl GstRc { pub fn make_mut(&mut self) -> &mut T { unsafe { if self.is_writable() { - return &mut *self.obj; + return self.obj.as_mut(); } - self.obj = T::from_mut_ptr( + let ptr = T::from_mut_ptr( ffi::gst_mini_object_make_writable(self.as_mut_ptr() as *mut ffi::GstMiniObject) as *mut T::GstType, ); + self.obj = ptr::NonNull::new_unchecked(ptr); assert!(self.is_writable()); - &mut *self.obj + self.obj.as_mut() } } pub fn get_mut(&mut self) -> Option<&mut T> { if self.is_writable() { - Some(unsafe { &mut *self.obj }) + Some(unsafe { self.obj.as_mut() }) } else { None } @@ -107,7 +108,7 @@ impl ops::Deref for GstRc { impl AsRef for GstRc { fn as_ref(&self) -> &T { - unsafe { &*self.obj } + unsafe { self.obj.as_ref() } } } diff --git a/gstreamer/src/static_caps.rs b/gstreamer/src/static_caps.rs index a234bd055..8551490a4 100644 --- a/gstreamer/src/static_caps.rs +++ b/gstreamer/src/static_caps.rs @@ -15,12 +15,14 @@ use gobject_ffi; use glib; use glib::translate::{from_glib_full, FromGlibPtrNone, ToGlibPtr, ToGlibPtrMut}; +use std::ptr; + #[repr(C)] -pub struct StaticCaps(*mut ffi::GstStaticCaps); +pub struct StaticCaps(ptr::NonNull); impl StaticCaps { pub fn get(&self) -> Caps { - unsafe { from_glib_full(ffi::gst_static_caps_get(self.0)) } + unsafe { from_glib_full(ffi::gst_static_caps_get(self.0.as_ptr())) } } } @@ -72,7 +74,7 @@ impl<'a> glib::translate::ToGlibPtr<'a, *const ffi::GstStaticCaps> for StaticCap type Storage = &'a StaticCaps; fn to_glib_none(&'a self) -> glib::translate::Stash<'a, *const ffi::GstStaticCaps, Self> { - glib::translate::Stash(self.0, self) + glib::translate::Stash(self.0.as_ptr(), self) } fn to_glib_full(&self) -> *const ffi::GstStaticCaps { @@ -84,7 +86,8 @@ impl<'a> glib::translate::ToGlibPtr<'a, *const ffi::GstStaticCaps> for StaticCap impl glib::translate::FromGlibPtrNone<*const ffi::GstStaticCaps> for StaticCaps { #[inline] unsafe fn from_glib_none(ptr: *const ffi::GstStaticCaps) -> Self { - StaticCaps(ptr as *mut _) + assert!(!ptr.is_null()); + StaticCaps(ptr::NonNull::new_unchecked(ptr as *mut _)) } } @@ -92,7 +95,8 @@ impl glib::translate::FromGlibPtrNone<*const ffi::GstStaticCaps> for StaticCaps impl glib::translate::FromGlibPtrNone<*mut ffi::GstStaticCaps> for StaticCaps { #[inline] unsafe fn from_glib_none(ptr: *mut ffi::GstStaticCaps) -> Self { - StaticCaps(ptr) + assert!(!ptr.is_null()); + StaticCaps(ptr::NonNull::new_unchecked(ptr)) } } @@ -100,7 +104,8 @@ impl glib::translate::FromGlibPtrNone<*mut ffi::GstStaticCaps> for StaticCaps { impl glib::translate::FromGlibPtrBorrow<*mut ffi::GstStaticCaps> for StaticCaps { #[inline] unsafe fn from_glib_borrow(ptr: *mut ffi::GstStaticCaps) -> Self { - StaticCaps(ptr) + assert!(!ptr.is_null()); + StaticCaps(ptr::NonNull::new_unchecked(ptr)) } } diff --git a/gstreamer/src/static_pad_template.rs b/gstreamer/src/static_pad_template.rs index a28805a58..f439e0dfc 100644 --- a/gstreamer/src/static_pad_template.rs +++ b/gstreamer/src/static_pad_template.rs @@ -17,28 +17,34 @@ use std::ffi::CStr; use glib; use glib::translate::{from_glib, from_glib_full, FromGlibPtrNone, ToGlibPtr, ToGlibPtrMut}; +use std::ptr; + #[repr(C)] -pub struct StaticPadTemplate(*mut ffi::GstStaticPadTemplate); +pub struct StaticPadTemplate(ptr::NonNull); impl StaticPadTemplate { pub fn get(&self) -> PadTemplate { - unsafe { from_glib_full(ffi::gst_static_pad_template_get(self.0)) } + unsafe { from_glib_full(ffi::gst_static_pad_template_get(self.0.as_ptr())) } } pub fn get_caps(&self) -> Caps { - unsafe { from_glib_full(ffi::gst_static_pad_template_get_caps(self.0)) } + unsafe { from_glib_full(ffi::gst_static_pad_template_get_caps(self.0.as_ptr())) } } pub fn name_template<'a>(&self) -> &'a str { - unsafe { CStr::from_ptr((*self.0).name_template).to_str().unwrap() } + unsafe { + CStr::from_ptr(self.0.as_ref().name_template) + .to_str() + .unwrap() + } } pub fn direction(&self) -> ::PadDirection { - unsafe { from_glib((*self.0).direction) } + unsafe { from_glib(self.0.as_ref().direction) } } pub fn presence(&self) -> ::PadPresence { - unsafe { from_glib((*self.0).presence) } + unsafe { from_glib(self.0.as_ref().presence) } } } @@ -94,7 +100,7 @@ impl<'a> glib::translate::ToGlibPtr<'a, *const ffi::GstStaticPadTemplate> for St fn to_glib_none( &'a self, ) -> glib::translate::Stash<'a, *const ffi::GstStaticPadTemplate, Self> { - glib::translate::Stash(self.0, self) + glib::translate::Stash(self.0.as_ptr(), self) } fn to_glib_full(&self) -> *const ffi::GstStaticPadTemplate { @@ -106,7 +112,8 @@ impl<'a> glib::translate::ToGlibPtr<'a, *const ffi::GstStaticPadTemplate> for St impl glib::translate::FromGlibPtrNone<*const ffi::GstStaticPadTemplate> for StaticPadTemplate { #[inline] unsafe fn from_glib_none(ptr: *const ffi::GstStaticPadTemplate) -> Self { - StaticPadTemplate(ptr as *mut _) + assert!(!ptr.is_null()); + StaticPadTemplate(ptr::NonNull::new_unchecked(ptr as *mut _)) } } @@ -114,7 +121,8 @@ impl glib::translate::FromGlibPtrNone<*const ffi::GstStaticPadTemplate> for Stat impl glib::translate::FromGlibPtrNone<*mut ffi::GstStaticPadTemplate> for StaticPadTemplate { #[inline] unsafe fn from_glib_none(ptr: *mut ffi::GstStaticPadTemplate) -> Self { - StaticPadTemplate(ptr) + assert!(!ptr.is_null()); + StaticPadTemplate(ptr::NonNull::new_unchecked(ptr)) } } @@ -122,7 +130,8 @@ impl glib::translate::FromGlibPtrNone<*mut ffi::GstStaticPadTemplate> for Static impl glib::translate::FromGlibPtrBorrow<*mut ffi::GstStaticPadTemplate> for StaticPadTemplate { #[inline] unsafe fn from_glib_borrow(ptr: *mut ffi::GstStaticPadTemplate) -> Self { - StaticPadTemplate(ptr) + assert!(!ptr.is_null()); + StaticPadTemplate(ptr::NonNull::new_unchecked(ptr)) } } diff --git a/gstreamer/src/structure.rs b/gstreamer/src/structure.rs index 8af37b0e5..1cc85c7e2 100644 --- a/gstreamer/src/structure.rs +++ b/gstreamer/src/structure.rs @@ -25,7 +25,7 @@ use ffi; use glib_ffi::gpointer; use gobject_ffi; -pub struct Structure(*mut StructureRef, PhantomData); +pub struct Structure(ptr::NonNull, PhantomData); unsafe impl Send for Structure {} impl Structure { @@ -36,10 +36,11 @@ impl Structure { pub fn new_empty(name: &str) -> Structure { assert_initialized_main_thread!(); - Structure( - unsafe { ffi::gst_structure_new_empty(name.to_glib_none().0) as *mut StructureRef }, - PhantomData, - ) + unsafe { + let ptr = ffi::gst_structure_new_empty(name.to_glib_none().0) as *mut StructureRef; + assert!(!ptr.is_null()); + Structure(ptr::NonNull::new_unchecked(ptr), PhantomData) + } } pub fn new(name: &str, values: &[(&str, &ToSendValue)]) -> Structure { @@ -60,13 +61,16 @@ impl Structure { if structure.is_null() { None } else { - Some(Structure(structure as *mut StructureRef, PhantomData)) + Some(Structure( + ptr::NonNull::new_unchecked(structure as *mut StructureRef), + PhantomData, + )) } } } pub unsafe fn into_ptr(self) -> *mut ffi::GstStructure { - let ptr = self.0 as *mut StructureRef as *mut ffi::GstStructure; + let ptr = self.0.as_ptr() as *mut StructureRef as *mut ffi::GstStructure; mem::forget(self); ptr @@ -77,13 +81,13 @@ impl Deref for Structure { type Target = StructureRef; fn deref(&self) -> &StructureRef { - unsafe { &*self.0 } + unsafe { self.0.as_ref() } } } impl DerefMut for Structure { fn deref_mut(&mut self) -> &mut StructureRef { - unsafe { &mut *self.0 } + unsafe { self.0.as_mut() } } } @@ -101,16 +105,17 @@ impl AsMut for Structure { impl Clone for Structure { fn clone(&self) -> Self { - Structure( - unsafe { ffi::gst_structure_copy(&(*self.0).0) as *mut StructureRef }, - PhantomData, - ) + unsafe { + let ptr = ffi::gst_structure_copy(&self.0.as_ref().0) as *mut StructureRef; + assert!(!ptr.is_null()); + Structure(ptr::NonNull::new_unchecked(ptr), PhantomData) + } } } impl Drop for Structure { fn drop(&mut self) { - unsafe { ffi::gst_structure_free(&mut (*self.0).0) } + unsafe { ffi::gst_structure_free(&mut self.0.as_mut().0) } } } @@ -151,13 +156,13 @@ impl str::FromStr for Structure { impl Borrow for Structure { fn borrow(&self) -> &StructureRef { - unsafe { &*self.0 } + unsafe { self.0.as_ref() } } } impl BorrowMut for Structure { fn borrow_mut(&mut self) -> &mut StructureRef { - unsafe { &mut *self.0 } + unsafe { self.0.as_mut() } } } @@ -165,10 +170,11 @@ impl ToOwned for StructureRef { type Owned = Structure; fn to_owned(&self) -> Structure { - Structure( - unsafe { ffi::gst_structure_copy(&self.0) as *mut StructureRef }, - PhantomData, - ) + unsafe { + let ptr = ffi::gst_structure_copy(&self.0) as *mut StructureRef; + assert!(!ptr.is_null()); + Structure(ptr::NonNull::new_unchecked(ptr), PhantomData) + } } } @@ -182,11 +188,11 @@ impl<'a> ToGlibPtr<'a, *const ffi::GstStructure> for Structure { type Storage = &'a Self; fn to_glib_none(&'a self) -> Stash<'a, *const ffi::GstStructure, Self> { - Stash(unsafe { &(*self.0).0 }, self) + unsafe { Stash(&self.0.as_ref().0, self) } } fn to_glib_full(&self) -> *const ffi::GstStructure { - unsafe { ffi::gst_structure_copy(&(*self.0).0) } + unsafe { ffi::gst_structure_copy(&self.0.as_ref().0) } } } @@ -194,11 +200,11 @@ impl<'a> ToGlibPtr<'a, *mut ffi::GstStructure> for Structure { type Storage = &'a Self; fn to_glib_none(&'a self) -> Stash<'a, *mut ffi::GstStructure, Self> { - Stash(unsafe { &mut (*self.0).0 }, self) + unsafe { Stash(&self.0.as_ref().0 as *const _ as *mut _, self) } } fn to_glib_full(&self) -> *mut ffi::GstStructure { - unsafe { ffi::gst_structure_copy(&(*self.0).0) } + unsafe { ffi::gst_structure_copy(&self.0.as_ref().0) } } } @@ -206,14 +212,17 @@ impl<'a> ToGlibPtrMut<'a, *mut ffi::GstStructure> for Structure { type Storage = &'a mut Self; fn to_glib_none_mut(&'a mut self) -> StashMut<*mut ffi::GstStructure, Self> { - StashMut(unsafe { &mut (*self.0).0 }, self) + unsafe { StashMut(&mut self.0.as_mut().0, self) } } } impl FromGlibPtrNone<*const ffi::GstStructure> for Structure { unsafe fn from_glib_none(ptr: *const ffi::GstStructure) -> Self { + assert!(!ptr.is_null()); + let ptr = ffi::gst_structure_copy(ptr); + assert!(!ptr.is_null()); Structure( - ffi::gst_structure_copy(ptr) as *mut StructureRef, + ptr::NonNull::new_unchecked(ptr as *mut StructureRef), PhantomData, ) } @@ -221,8 +230,11 @@ impl FromGlibPtrNone<*const ffi::GstStructure> for Structure { impl FromGlibPtrNone<*mut ffi::GstStructure> for Structure { unsafe fn from_glib_none(ptr: *mut ffi::GstStructure) -> Self { + assert!(!ptr.is_null()); + let ptr = ffi::gst_structure_copy(ptr); + assert!(!ptr.is_null()); Structure( - ffi::gst_structure_copy(ptr) as *mut StructureRef, + ptr::NonNull::new_unchecked(ptr as *mut StructureRef), PhantomData, ) } @@ -230,26 +242,35 @@ impl FromGlibPtrNone<*mut ffi::GstStructure> for Structure { impl FromGlibPtrFull<*const ffi::GstStructure> for Structure { unsafe fn from_glib_full(ptr: *const ffi::GstStructure) -> Self { - Structure(ptr as *mut StructureRef, PhantomData) + assert!(!ptr.is_null()); + Structure( + ptr::NonNull::new_unchecked(ptr as *mut StructureRef), + PhantomData, + ) } } impl FromGlibPtrFull<*mut ffi::GstStructure> for Structure { unsafe fn from_glib_full(ptr: *mut ffi::GstStructure) -> Self { - Structure(ptr as *mut StructureRef, PhantomData) + assert!(!ptr.is_null()); + Structure( + ptr::NonNull::new_unchecked(ptr as *mut StructureRef), + PhantomData, + ) } } impl<'a> glib::value::FromValueOptional<'a> for Structure { unsafe fn from_value_optional(v: &'a glib::Value) -> Option { let ptr = gobject_ffi::g_value_get_boxed(v.to_glib_none().0); + assert!(!ptr.is_null()); from_glib_none(ptr as *const ffi::GstStructure) } } impl glib::value::SetValue for Structure { unsafe fn set_value(v: &mut glib::Value, s: &Self) { - gobject_ffi::g_value_set_boxed(v.to_glib_none_mut().0, s.0 as gpointer); + gobject_ffi::g_value_set_boxed(v.to_glib_none_mut().0, s.0.as_ptr() as gpointer); } }