Use raw pointers instead of &'static references

&'static is wrong here is the pointers are only valid for the lifetime
of the surrounding struct.

Also place a PhantomData<T> inside the structs as conceptually we own a
T, see std::ptr::Unique and
https://github.com/rust-lang/rfcs/blob/master/text/0769-sound-generic-drop.md#phantom-data

Need to add the PhantomData<T> elsewhere too.
This commit is contained in:
Sebastian Dröge 2017-04-27 12:33:28 +03:00
parent c6addbfe48
commit 52981968f6
2 changed files with 39 additions and 38 deletions

View file

@ -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<T: 'static + MiniObject> {
obj: &'static T,
}
pub struct GstRc<T: MiniObject>(*mut T, PhantomData<T>);
impl<T: MiniObject> GstRc<T> {
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<T: MiniObject> GstRc<T> {
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<T: MiniObject> GstRc<T> {
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<T: MiniObject> GstRc<T> {
} == 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<T: MiniObject> GstRc<T> {
impl<T: MiniObject> ops::Deref for GstRc<T> {
type Target = T;
fn deref(&self) -> &T {
self.obj
self.as_ref()
}
}
impl<T: MiniObject> AsRef<T> for GstRc<T> {
fn as_ref(&self) -> &T {
self.obj
unsafe { &*self.0 }
}
}
impl<T: MiniObject> borrow::Borrow<T> for GstRc<T> {
fn borrow(&self) -> &T {
self.obj
self.as_ref()
}
}
@ -111,14 +110,14 @@ impl<T: MiniObject> borrow::Borrow<T> for GstRc<T> {
impl<T: MiniObject> Clone for GstRc<T> {
fn clone(&self) -> GstRc<T> {
unsafe { GstRc::from_unowned_ptr(self.obj.as_ptr()) }
unsafe { GstRc::from_unowned_ptr(self.as_ptr()) }
}
}
impl<T: MiniObject> Drop for GstRc<T> {
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<T: MiniObject + Send> Send for GstRc<T> {}
impl<T: MiniObject + fmt::Display> fmt::Display for GstRc<T> {
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)
}
}

View file

@ -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<Structure>);
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<Structure> 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<Structure> for OwnedStructure {
fn borrow(&self) -> &Structure {
self.0
unsafe { &*self.0 }
}
}
impl BorrowMut<Structure> 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)
}
}