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.
This commit is contained in:
Sebastian Dröge 2018-09-30 01:17:12 +03:00
parent 7270a837d1
commit 6d5bded7b3
3 changed files with 52 additions and 53 deletions

View file

@ -7,7 +7,6 @@
// except according to those terms. // except according to those terms.
use std::fmt; use std::fmt;
use std::marker::PhantomData;
use ffi; use ffi;
use glib; use glib;
@ -18,10 +17,10 @@ use gst::prelude::*;
use gst_ffi; use gst_ffi;
#[repr(C)] #[repr(C)]
pub struct VideoMeta<'a>(ffi::GstVideoMeta, PhantomData<&'a ()>); pub struct VideoMeta(ffi::GstVideoMeta);
impl<'a> VideoMeta<'a> { impl VideoMeta {
pub fn add( pub fn add<'a>(
buffer: &'a mut gst::BufferRef, buffer: &'a mut gst::BufferRef,
flags: ::VideoFrameFlags, flags: ::VideoFrameFlags,
format: ::VideoFormat, format: ::VideoFormat,
@ -40,11 +39,11 @@ impl<'a> VideoMeta<'a> {
height, 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, buffer: &'a mut gst::BufferRef,
flags: ::VideoFrameFlags, flags: ::VideoFrameFlags,
format: ::VideoFormat, format: ::VideoFormat,
@ -73,7 +72,7 @@ impl<'a> VideoMeta<'a> {
stride.as_ptr() as *mut _, 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; type GstType = ffi::GstVideoMeta;
fn get_meta_api() -> glib::Type { 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 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("VideoMeta") f.debug_struct("VideoMeta")
.field("id", &self.get_id()) .field("id", &self.get_id())
@ -134,13 +133,10 @@ impl<'a> fmt::Debug for VideoMeta<'a> {
} }
#[repr(C)] #[repr(C)]
pub struct VideoOverlayCompositionMeta<'a>( pub struct VideoOverlayCompositionMeta(ffi::GstVideoOverlayCompositionMeta);
ffi::GstVideoOverlayCompositionMeta,
PhantomData<&'a ()>,
);
impl<'a> VideoOverlayCompositionMeta<'a> { impl VideoOverlayCompositionMeta {
pub fn add( pub fn add<'a>(
buffer: &'a mut gst::BufferRef, buffer: &'a mut gst::BufferRef,
overlay: ::VideoOverlayComposition, overlay: ::VideoOverlayComposition,
) -> gst::MetaRefMut<'a, Self, gst::meta::Standalone> { ) -> gst::MetaRefMut<'a, Self, gst::meta::Standalone> {
@ -150,7 +146,7 @@ impl<'a> VideoOverlayCompositionMeta<'a> {
overlay.as_mut_ptr(), 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; type GstType = ffi::GstVideoOverlayCompositionMeta;
fn get_meta_api() -> glib::Type { 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 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("VideoOverlayCompositionMeta") f.debug_struct("VideoOverlayCompositionMeta")
.field("overlay", &self.get_overlay()) .field("overlay", &self.get_overlay())

View file

@ -375,10 +375,7 @@ impl BufferRef {
if meta.is_null() { if meta.is_null() {
None None
} else { } else {
Some(T::from_ptr( Some(T::from_ptr(self, meta as *const <T as MetaAPI>::GstType))
self.as_ptr(),
meta as *const <T as MetaAPI>::GstType,
))
} }
} }
} }
@ -389,10 +386,7 @@ impl BufferRef {
if meta.is_null() { if meta.is_null() {
None None
} else { } else {
Some(T::from_mut_ptr( Some(T::from_mut_ptr(self, meta as *mut <T as MetaAPI>::GstType))
self.as_mut_ptr(),
meta as *mut <T as MetaAPI>::GstType,
))
} }
} }
} }
@ -407,7 +401,7 @@ impl BufferRef {
} }
macro_rules! define_iter( 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> { pub struct $name<'a, T: MetaAPI + 'a> {
buffer: $typ, buffer: $typ,
state: glib_ffi::gpointer, state: glib_ffi::gpointer,
@ -439,7 +433,9 @@ macro_rules! define_iter(
if meta.is_null() { if meta.is_null() {
return None; return None;
} else if self.meta_api == glib::Type::Invalid || glib::Type::from_glib((*(*meta).info).api) == self.meta_api { } 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); return Some(item);
} }
} }
@ -451,14 +447,19 @@ macro_rules! define_iter(
} }
); );
define_iter!(MetaIter, &'a BufferRef, MetaRef<'a, T>, |buffer, meta| { define_iter!(
T::from_ptr(buffer, meta as *const <T as MetaAPI>::GstType) MetaIter,
}); &'a BufferRef,
MetaRef<'a, T>,
|buffer: *const ffi::GstBuffer| BufferRef::from_ptr(buffer),
|buffer, meta| T::from_ptr(buffer, meta as *const <T as MetaAPI>::GstType)
);
define_iter!( define_iter!(
MetaIterMut, MetaIterMut,
&'a mut BufferRef, &'a mut BufferRef,
MetaRefMut<'a, T, ::meta::Iterated>, MetaRefMut<'a, T, ::meta::Iterated>,
|buffer, meta| T::from_mut_ptr(buffer, meta as *mut <T as MetaAPI>::GstType) |buffer: *mut ffi::GstBuffer| BufferRef::from_mut_ptr(buffer),
|buffer: &'a mut BufferRef, meta| T::from_mut_ptr(buffer, meta as *mut <T as MetaAPI>::GstType)
); );
impl fmt::Debug for BufferRef { impl fmt::Debug for BufferRef {

View file

@ -24,10 +24,7 @@ pub unsafe trait MetaAPI: Sized {
fn get_meta_api() -> glib::Type; fn get_meta_api() -> glib::Type;
unsafe fn from_ptr<'a>( unsafe fn from_ptr<'a>(buffer: &'a BufferRef, ptr: *const Self::GstType) -> MetaRef<'a, Self> {
buffer: *const ffi::GstBuffer,
ptr: *const Self::GstType,
) -> MetaRef<'a, Self> {
assert!(!ptr.is_null()); assert!(!ptr.is_null());
let meta_api = Self::get_meta_api(); let meta_api = Self::get_meta_api();
@ -45,7 +42,7 @@ pub unsafe trait MetaAPI: Sized {
} }
unsafe fn from_mut_ptr<'a, T>( unsafe fn from_mut_ptr<'a, T>(
buffer: *mut ffi::GstBuffer, buffer: &'a mut BufferRef,
ptr: *mut Self::GstType, ptr: *mut Self::GstType,
) -> MetaRefMut<'a, Self, T> { ) -> MetaRefMut<'a, Self, T> {
assert!(!ptr.is_null()); assert!(!ptr.is_null());
@ -69,7 +66,7 @@ pub unsafe trait MetaAPI: Sized {
#[derive(Debug)] #[derive(Debug)]
pub struct MetaRef<'a, T: MetaAPI + 'a> { pub struct MetaRef<'a, T: MetaAPI + 'a> {
meta: &'a T, meta: &'a T,
buffer: *const ffi::GstBuffer, buffer: &'a BufferRef,
} }
pub enum Standalone {} pub enum Standalone {}
@ -78,7 +75,7 @@ pub enum Iterated {}
#[derive(Debug)] #[derive(Debug)]
pub struct MetaRefMut<'a, T: MetaAPI + 'a, U> { pub struct MetaRefMut<'a, T: MetaAPI + 'a, U> {
meta: &'a mut T, meta: &'a mut T,
buffer: *mut ffi::GstBuffer, buffer: &'a mut BufferRef,
mode: PhantomData<U>, mode: PhantomData<U>,
} }
@ -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<T: MetaAPI>(&self) -> Option<&MetaRef<'a, T>> { pub fn downcast_ref<T: MetaAPI>(&self) -> Option<&MetaRef<'a, T>> {
let target_type = T::get_meta_api(); let target_type = T::get_meta_api();
let type_ = self.get_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> { impl<'a, T: MetaAPI> MetaRefMut<'a, T, Standalone> {
pub fn remove(mut self) { pub fn remove(mut self) {
unsafe { unsafe {
let res = let res = ffi::gst_buffer_remove_meta(
ffi::gst_buffer_remove_meta(self.buffer, self.as_mut_ptr() as *mut ffi::GstMeta); self.buffer.as_mut_ptr(),
self.as_mut_ptr() as *mut ffi::GstMeta,
);
assert_ne!(res, glib_ffi::GFALSE); 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<T: MetaAPI>(&mut self) -> Option<&MetaRefMut<'a, T, U>> { pub fn downcast_ref<T: MetaAPI>(&mut self) -> Option<&MetaRefMut<'a, T, U>> {
let target_type = T::get_meta_api(); let target_type = T::get_meta_api();
let type_ = self.get_api(); let type_ = self.get_api();
@ -185,15 +184,15 @@ impl<'a, U> MetaRefMut<'a, Meta<'a>, U> {
} }
#[repr(C)] #[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 { fn get_api(&self) -> glib::Type {
unsafe { glib::Type::from_glib((*self.0.info).api) } 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; type GstType = ffi::GstMeta;
fn get_meta_api() -> glib::Type { 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 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Meta") f.debug_struct("Meta")
.field("api", &self.get_api()) .field("api", &self.get_api())
@ -210,15 +209,18 @@ impl<'a> fmt::Debug for Meta<'a> {
} }
#[repr(C)] #[repr(C)]
pub struct ParentBufferMeta<'a>(ffi::GstParentBufferMeta, PhantomData<&'a ()>); pub struct ParentBufferMeta(ffi::GstParentBufferMeta);
impl<'a> ParentBufferMeta<'a> { impl ParentBufferMeta {
pub fn add(buffer: &'a mut BufferRef, parent: &BufferRef) -> MetaRefMut<'a, Self, Standalone> { pub fn add<'a>(
buffer: &'a mut BufferRef,
parent: &BufferRef,
) -> MetaRefMut<'a, Self, Standalone> {
unsafe { unsafe {
let meta = let meta =
ffi::gst_buffer_add_parent_buffer_meta(buffer.as_mut_ptr(), parent.as_mut_ptr()); 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; type GstType = ffi::GstParentBufferMeta;
fn get_meta_api() -> glib::Type { 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 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("ParentBufferMeta") f.debug_struct("ParentBufferMeta")
.field("parent", &self.get_parent()) .field("parent", &self.get_parent())