From 6d5bded7b39cf4d304196f3c86fe4f9909363b52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Sun, 30 Sep 2018 01:17:12 +0300 Subject: [PATCH] Get rid of lifetime for the Meta structs The structs themselves don't reference the buffer, the reference to these structs is borrowed from the buffer. --- gstreamer-video/src/video_meta.rs | 32 ++++++++++------------ gstreamer/src/buffer.rs | 29 ++++++++++---------- gstreamer/src/meta.rs | 44 ++++++++++++++++--------------- 3 files changed, 52 insertions(+), 53 deletions(-) diff --git a/gstreamer-video/src/video_meta.rs b/gstreamer-video/src/video_meta.rs index e925f209a..12950ea7d 100644 --- a/gstreamer-video/src/video_meta.rs +++ b/gstreamer-video/src/video_meta.rs @@ -7,7 +7,6 @@ // except according to those terms. use std::fmt; -use std::marker::PhantomData; use ffi; use glib; @@ -18,10 +17,10 @@ use gst::prelude::*; use gst_ffi; #[repr(C)] -pub struct VideoMeta<'a>(ffi::GstVideoMeta, PhantomData<&'a ()>); +pub struct VideoMeta(ffi::GstVideoMeta); -impl<'a> VideoMeta<'a> { - pub fn add( +impl VideoMeta { + pub fn add<'a>( buffer: &'a mut gst::BufferRef, flags: ::VideoFrameFlags, format: ::VideoFormat, @@ -40,11 +39,11 @@ impl<'a> VideoMeta<'a> { height, ); - Self::from_mut_ptr(buffer.as_mut_ptr(), meta) + Self::from_mut_ptr(buffer, meta) } } - pub fn add_full( + pub fn add_full<'a>( buffer: &'a mut gst::BufferRef, flags: ::VideoFrameFlags, format: ::VideoFormat, @@ -73,7 +72,7 @@ impl<'a> VideoMeta<'a> { stride.as_ptr() as *mut _, ); - Self::from_mut_ptr(buffer.as_mut_ptr(), meta) + Self::from_mut_ptr(buffer, meta) } } @@ -110,7 +109,7 @@ impl<'a> VideoMeta<'a> { } } -unsafe impl<'a> MetaAPI for VideoMeta<'a> { +unsafe impl MetaAPI for VideoMeta { type GstType = ffi::GstVideoMeta; fn get_meta_api() -> glib::Type { @@ -118,7 +117,7 @@ unsafe impl<'a> MetaAPI for VideoMeta<'a> { } } -impl<'a> fmt::Debug for VideoMeta<'a> { +impl fmt::Debug for VideoMeta { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("VideoMeta") .field("id", &self.get_id()) @@ -134,13 +133,10 @@ impl<'a> fmt::Debug for VideoMeta<'a> { } #[repr(C)] -pub struct VideoOverlayCompositionMeta<'a>( - ffi::GstVideoOverlayCompositionMeta, - PhantomData<&'a ()>, -); +pub struct VideoOverlayCompositionMeta(ffi::GstVideoOverlayCompositionMeta); -impl<'a> VideoOverlayCompositionMeta<'a> { - pub fn add( +impl VideoOverlayCompositionMeta { + pub fn add<'a>( buffer: &'a mut gst::BufferRef, overlay: ::VideoOverlayComposition, ) -> gst::MetaRefMut<'a, Self, gst::meta::Standalone> { @@ -150,7 +146,7 @@ impl<'a> VideoOverlayCompositionMeta<'a> { overlay.as_mut_ptr(), ); - Self::from_mut_ptr(buffer.as_mut_ptr(), meta) + Self::from_mut_ptr(buffer, meta) } } @@ -177,7 +173,7 @@ impl<'a> VideoOverlayCompositionMeta<'a> { } } -unsafe impl<'a> MetaAPI for VideoOverlayCompositionMeta<'a> { +unsafe impl MetaAPI for VideoOverlayCompositionMeta { type GstType = ffi::GstVideoOverlayCompositionMeta; fn get_meta_api() -> glib::Type { @@ -185,7 +181,7 @@ unsafe impl<'a> MetaAPI for VideoOverlayCompositionMeta<'a> { } } -impl<'a> fmt::Debug for VideoOverlayCompositionMeta<'a> { +impl fmt::Debug for VideoOverlayCompositionMeta { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("VideoOverlayCompositionMeta") .field("overlay", &self.get_overlay()) diff --git a/gstreamer/src/buffer.rs b/gstreamer/src/buffer.rs index 508f41da3..5cdfce4cd 100644 --- a/gstreamer/src/buffer.rs +++ b/gstreamer/src/buffer.rs @@ -375,10 +375,7 @@ impl BufferRef { if meta.is_null() { None } else { - Some(T::from_ptr( - self.as_ptr(), - meta as *const ::GstType, - )) + Some(T::from_ptr(self, meta as *const ::GstType)) } } } @@ -389,10 +386,7 @@ impl BufferRef { if meta.is_null() { None } else { - Some(T::from_mut_ptr( - self.as_mut_ptr(), - meta as *mut ::GstType, - )) + Some(T::from_mut_ptr(self, meta as *mut ::GstType)) } } } @@ -407,7 +401,7 @@ impl BufferRef { } macro_rules! define_iter( - ($name:ident, $typ:ty, $mtyp:ty, $from_ptr:expr) => { + ($name:ident, $typ:ty, $mtyp:ty, $prepare_buffer:expr, $from_ptr:expr) => { pub struct $name<'a, T: MetaAPI + 'a> { buffer: $typ, state: glib_ffi::gpointer, @@ -439,7 +433,9 @@ macro_rules! define_iter( if meta.is_null() { return None; } else if self.meta_api == glib::Type::Invalid || glib::Type::from_glib((*(*meta).info).api) == self.meta_api { - let item = $from_ptr(self.buffer.as_mut_ptr(), meta); + // FIXME: Workaround for a lifetime issue with the mutable iterator only + let buffer = $prepare_buffer(self.buffer.as_mut_ptr()); + let item = $from_ptr(buffer, meta); return Some(item); } } @@ -451,14 +447,19 @@ macro_rules! define_iter( } ); -define_iter!(MetaIter, &'a BufferRef, MetaRef<'a, T>, |buffer, meta| { - T::from_ptr(buffer, meta as *const ::GstType) -}); +define_iter!( + MetaIter, + &'a BufferRef, + MetaRef<'a, T>, + |buffer: *const ffi::GstBuffer| BufferRef::from_ptr(buffer), + |buffer, meta| T::from_ptr(buffer, meta as *const ::GstType) +); define_iter!( MetaIterMut, &'a mut BufferRef, MetaRefMut<'a, T, ::meta::Iterated>, - |buffer, meta| T::from_mut_ptr(buffer, meta as *mut ::GstType) + |buffer: *mut ffi::GstBuffer| BufferRef::from_mut_ptr(buffer), + |buffer: &'a mut BufferRef, meta| T::from_mut_ptr(buffer, meta as *mut ::GstType) ); impl fmt::Debug for BufferRef { diff --git a/gstreamer/src/meta.rs b/gstreamer/src/meta.rs index d476b0d46..ce639b472 100644 --- a/gstreamer/src/meta.rs +++ b/gstreamer/src/meta.rs @@ -24,10 +24,7 @@ pub unsafe trait MetaAPI: Sized { fn get_meta_api() -> glib::Type; - unsafe fn from_ptr<'a>( - buffer: *const ffi::GstBuffer, - ptr: *const Self::GstType, - ) -> MetaRef<'a, Self> { + unsafe fn from_ptr<'a>(buffer: &'a BufferRef, ptr: *const Self::GstType) -> MetaRef<'a, Self> { assert!(!ptr.is_null()); let meta_api = Self::get_meta_api(); @@ -45,7 +42,7 @@ pub unsafe trait MetaAPI: Sized { } unsafe fn from_mut_ptr<'a, T>( - buffer: *mut ffi::GstBuffer, + buffer: &'a mut BufferRef, ptr: *mut Self::GstType, ) -> MetaRefMut<'a, Self, T> { assert!(!ptr.is_null()); @@ -69,7 +66,7 @@ pub unsafe trait MetaAPI: Sized { #[derive(Debug)] pub struct MetaRef<'a, T: MetaAPI + 'a> { meta: &'a T, - buffer: *const ffi::GstBuffer, + buffer: &'a BufferRef, } pub enum Standalone {} @@ -78,7 +75,7 @@ pub enum Iterated {} #[derive(Debug)] pub struct MetaRefMut<'a, T: MetaAPI + 'a, U> { meta: &'a mut T, - buffer: *mut ffi::GstBuffer, + buffer: &'a mut BufferRef, mode: PhantomData, } @@ -130,7 +127,7 @@ impl<'a, T: MetaAPI> MetaRef<'a, T> { } } -impl<'a> MetaRef<'a, Meta<'a>> { +impl<'a> MetaRef<'a, Meta> { pub fn downcast_ref(&self) -> Option<&MetaRef<'a, T>> { let target_type = T::get_meta_api(); let type_ = self.get_api(); @@ -164,14 +161,16 @@ impl<'a, T: MetaAPI, U> MetaRefMut<'a, T, U> { impl<'a, T: MetaAPI> MetaRefMut<'a, T, Standalone> { pub fn remove(mut self) { unsafe { - let res = - ffi::gst_buffer_remove_meta(self.buffer, self.as_mut_ptr() as *mut ffi::GstMeta); + let res = ffi::gst_buffer_remove_meta( + self.buffer.as_mut_ptr(), + self.as_mut_ptr() as *mut ffi::GstMeta, + ); assert_ne!(res, glib_ffi::GFALSE); } } } -impl<'a, U> MetaRefMut<'a, Meta<'a>, U> { +impl<'a, U> MetaRefMut<'a, Meta, U> { pub fn downcast_ref(&mut self) -> Option<&MetaRefMut<'a, T, U>> { let target_type = T::get_meta_api(); let type_ = self.get_api(); @@ -185,15 +184,15 @@ impl<'a, U> MetaRefMut<'a, Meta<'a>, U> { } #[repr(C)] -pub struct Meta<'a>(ffi::GstMeta, PhantomData<&'a ()>); +pub struct Meta(ffi::GstMeta); -impl<'a> Meta<'a> { +impl Meta { fn get_api(&self) -> glib::Type { unsafe { glib::Type::from_glib((*self.0.info).api) } } } -unsafe impl<'a> MetaAPI for Meta<'a> { +unsafe impl MetaAPI for Meta { type GstType = ffi::GstMeta; fn get_meta_api() -> glib::Type { @@ -201,7 +200,7 @@ unsafe impl<'a> MetaAPI for Meta<'a> { } } -impl<'a> fmt::Debug for Meta<'a> { +impl fmt::Debug for Meta { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Meta") .field("api", &self.get_api()) @@ -210,15 +209,18 @@ impl<'a> fmt::Debug for Meta<'a> { } #[repr(C)] -pub struct ParentBufferMeta<'a>(ffi::GstParentBufferMeta, PhantomData<&'a ()>); +pub struct ParentBufferMeta(ffi::GstParentBufferMeta); -impl<'a> ParentBufferMeta<'a> { - pub fn add(buffer: &'a mut BufferRef, parent: &BufferRef) -> MetaRefMut<'a, Self, Standalone> { +impl ParentBufferMeta { + pub fn add<'a>( + buffer: &'a mut BufferRef, + parent: &BufferRef, + ) -> MetaRefMut<'a, Self, Standalone> { unsafe { let meta = ffi::gst_buffer_add_parent_buffer_meta(buffer.as_mut_ptr(), parent.as_mut_ptr()); - Self::from_mut_ptr(buffer.as_mut_ptr(), meta) + Self::from_mut_ptr(buffer, meta) } } @@ -227,7 +229,7 @@ impl<'a> ParentBufferMeta<'a> { } } -unsafe impl<'a> MetaAPI for ParentBufferMeta<'a> { +unsafe impl MetaAPI for ParentBufferMeta { type GstType = ffi::GstParentBufferMeta; fn get_meta_api() -> glib::Type { @@ -235,7 +237,7 @@ unsafe impl<'a> MetaAPI for ParentBufferMeta<'a> { } } -impl<'a> fmt::Debug for ParentBufferMeta<'a> { +impl fmt::Debug for ParentBufferMeta { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("ParentBufferMeta") .field("parent", &self.get_parent())