diff --git a/gst-plugin/src/miniobject.rs b/gst-plugin/src/miniobject.rs index c98d1da0..cb1a7e1c 100644 --- a/gst-plugin/src/miniobject.rs +++ b/gst-plugin/src/miniobject.rs @@ -8,22 +8,23 @@ use std::{fmt, ops, borrow}; use std::mem; +use std::marker::PhantomData; use glib; use gst; #[derive(Hash, Debug, PartialEq, Eq, PartialOrd, Ord)] -pub struct GstRc { - obj: &'static T, -} +pub struct GstRc(*mut T, PhantomData); impl GstRc { - unsafe fn new(obj: &'static T, owned: bool) -> Self { + unsafe fn new(obj: *const T, owned: bool) -> Self { + assert!(!obj.is_null()); + if !owned { - gst::gst_mini_object_ref(obj.as_ptr() as *mut gst::GstMiniObject); + gst::gst_mini_object_ref((&*obj).as_ptr() as *mut gst::GstMiniObject); } - GstRc { obj: obj } + GstRc(obj as *mut T, PhantomData) } pub unsafe fn from_owned_ptr(ptr: *const T::PtrType) -> Self { @@ -36,24 +37,22 @@ impl GstRc { pub fn make_mut(&mut self) -> &mut T { unsafe { - let ptr = self.obj.as_ptr(); - if self.is_writable() { - return &mut *(self.obj as *const T as *mut T); + return &mut *self.0; } - self.obj = T::from_ptr(gst::gst_mini_object_make_writable(ptr as - *mut gst::GstMiniObject) as - *const T::PtrType); + self.0 = T::from_mut_ptr(gst::gst_mini_object_make_writable(self.as_mut_ptr() as + *mut gst::GstMiniObject) as + *mut T::PtrType); assert!(self.is_writable()); - &mut *(self.obj as *const T as *mut T) + &mut *self.0 } } pub fn get_mut(&mut self) -> Option<&mut T> { if self.is_writable() { - Some(unsafe { &mut *(self.obj as *const T as *mut T) }) + Some(unsafe { &mut *self.0 }) } else { None } @@ -61,7 +60,7 @@ impl GstRc { pub fn copy(&self) -> Self { unsafe { - GstRc::from_owned_ptr(gst::gst_mini_object_copy(self.obj.as_ptr() as + GstRc::from_owned_ptr(gst::gst_mini_object_copy(self.as_ptr() as *const gst::GstMiniObject) as *const T::PtrType) } @@ -73,8 +72,8 @@ impl GstRc { } == glib::GTRUE) } - pub unsafe fn into_ptr(self) -> *const T::PtrType { - let ptr = self.obj.as_ptr(); + pub unsafe fn into_ptr(self) -> *mut T::PtrType { + let ptr = self.as_mut_ptr(); mem::forget(self); ptr @@ -84,19 +83,19 @@ impl GstRc { impl ops::Deref for GstRc { type Target = T; fn deref(&self) -> &T { - self.obj + self.as_ref() } } impl AsRef for GstRc { fn as_ref(&self) -> &T { - self.obj + unsafe { &*self.0 } } } impl borrow::Borrow for GstRc { fn borrow(&self) -> &T { - self.obj + self.as_ref() } } @@ -111,14 +110,14 @@ impl borrow::Borrow for GstRc { impl Clone for GstRc { fn clone(&self) -> GstRc { - unsafe { GstRc::from_unowned_ptr(self.obj.as_ptr()) } + unsafe { GstRc::from_unowned_ptr(self.as_ptr()) } } } impl Drop for GstRc { fn drop(&mut self) { unsafe { - gst::gst_mini_object_unref(self.obj.as_ptr() as *mut gst::GstMiniObject); + gst::gst_mini_object_unref(self.as_ptr() as *mut gst::GstMiniObject); } } } @@ -128,7 +127,7 @@ unsafe impl Send for GstRc {} impl fmt::Display for GstRc { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.obj.fmt(f) + (unsafe { &*self.0 }).fmt(f) } } @@ -155,4 +154,3 @@ pub unsafe trait MiniObject &mut *(ptr as *mut Self) } } - diff --git a/gst-plugin/src/structure.rs b/gst-plugin/src/structure.rs index 36cdc3dc..f5de2c18 100644 --- a/gst-plugin/src/structure.rs +++ b/gst-plugin/src/structure.rs @@ -12,21 +12,22 @@ use std::mem; use std::ffi::{CStr, CString}; use std::ops::{Deref, DerefMut}; use std::borrow::{Borrow, ToOwned, BorrowMut}; +use std::marker::PhantomData; use value::*; use glib; use gst; -pub struct OwnedStructure(&'static mut Structure); +pub struct OwnedStructure(*mut Structure, PhantomData); impl OwnedStructure { pub fn new_empty(name: &str) -> OwnedStructure { let name_cstr = CString::new(name).unwrap(); OwnedStructure(unsafe { - &mut *(gst::gst_structure_new_empty(name_cstr.as_ptr()) as - *mut Structure) - }) + gst::gst_structure_new_empty(name_cstr.as_ptr()) as *mut Structure + }, + PhantomData) } pub fn new(name: &str, values: &[(&str, Value)]) -> OwnedStructure { @@ -46,13 +47,13 @@ impl OwnedStructure { if structure.is_null() { None } else { - Some(OwnedStructure(&mut *(structure as *mut Structure))) + Some(OwnedStructure(structure as *mut Structure, PhantomData)) } } } - pub unsafe fn into_ptr(self) -> *const gst::GstStructure { - let ptr = self.0 as *const Structure as *const gst::GstStructure; + pub unsafe fn into_ptr(self) -> *mut gst::GstStructure { + let ptr = self.0 as *mut Structure as *mut gst::GstStructure; mem::forget(self); ptr @@ -63,13 +64,13 @@ impl Deref for OwnedStructure { type Target = Structure; fn deref(&self) -> &Structure { - self.0 + unsafe { &*self.0 } } } impl DerefMut for OwnedStructure { fn deref_mut(&mut self) -> &mut Structure { - self.0 + unsafe { &mut *self.0 } } } @@ -87,13 +88,14 @@ impl AsMut for OwnedStructure { impl Clone for OwnedStructure { fn clone(&self) -> Self { - OwnedStructure(unsafe { &mut *(gst::gst_structure_copy(&(self.0).0) as *mut Structure) }) + OwnedStructure(unsafe { gst::gst_structure_copy(&(*self.0).0) as *mut Structure }, + PhantomData) } } impl Drop for OwnedStructure { fn drop(&mut self) { - unsafe { gst::gst_structure_free(&mut (self.0).0) } + unsafe { gst::gst_structure_free(&mut (*self.0).0) } } } @@ -119,13 +121,13 @@ impl Eq for OwnedStructure {} impl Borrow for OwnedStructure { fn borrow(&self) -> &Structure { - self.0 + unsafe { &*self.0 } } } impl BorrowMut for OwnedStructure { fn borrow_mut(&mut self) -> &mut Structure { - self.0 + unsafe { &mut *self.0 } } } @@ -133,7 +135,8 @@ impl ToOwned for Structure { type Owned = OwnedStructure; fn to_owned(&self) -> OwnedStructure { - OwnedStructure(unsafe { &mut *(gst::gst_structure_copy(&self.0) as *mut Structure) }) + OwnedStructure(unsafe { gst::gst_structure_copy(&self.0) as *mut Structure }, + PhantomData) } }