Use our own reimplementation of mopa for our use cases

We had to replicate the mopafy! macro already anyway and poke into the
internals of the mopa implementation.
This commit is contained in:
Sebastian Dröge 2017-09-26 13:54:55 +03:00
parent e97f4fb8c1
commit 6a7da572f0
7 changed files with 90 additions and 43 deletions

View file

@ -10,7 +10,6 @@ libc = "0.2"
url = "1.1" url = "1.1"
lazy_static = "0.2" lazy_static = "0.2"
byteorder = "1.0" byteorder = "1.0"
mopa = "0.2"
glib-sys = { git = "https://github.com/gtk-rs/sys" } glib-sys = { git = "https://github.com/gtk-rs/sys" }
gobject-sys = { git = "https://github.com/gtk-rs/sys" } gobject-sys = { git = "https://github.com/gtk-rs/sys" }
gstreamer-sys = { git = "https://github.com/sdroege/gstreamer-sys", features = ["v1_10"] } gstreamer-sys = { git = "https://github.com/sdroege/gstreamer-sys", features = ["v1_10"] }

75
gst-plugin/src/anyimpl.rs Normal file
View file

@ -0,0 +1,75 @@
// Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// Heavily inspired by the mopa trait
use std::any::{Any, TypeId};
pub trait AnyImpl: Any {
fn get_type_id(&self) -> TypeId;
}
impl<T: Any> AnyImpl for T {
fn get_type_id(&self) -> TypeId {
TypeId::of::<T>()
}
}
macro_rules! any_impl {
($bound:ident, $trait:ident) => {
impl<T: $bound> $trait<T> {
#[inline]
pub fn downcast_ref<U: $trait<T>>(&self) -> Option<&U> {
if self.is::<U>() {
unsafe {
Some(self.downcast_ref_unchecked())
}
} else {
None
}
}
#[inline]
pub unsafe fn downcast_ref_unchecked<U: $trait<T>>(&self) -> &U {
&*(self as *const Self as *const U)
}
#[inline]
pub fn is<U: $trait<T>>(&self) -> bool {
use std::any::TypeId;
TypeId::of::<U>() == $crate::anyimpl::AnyImpl::get_type_id(self)
}
}
};
($trait:ident) => {
impl $trait {
#[inline]
pub fn downcast_ref<U: $trait>(&self) -> Option<&U> {
if self.is::<U>() {
unsafe {
Some(self.downcast_ref_unchecked())
}
} else {
None
}
}
#[inline]
pub unsafe fn downcast_ref_unchecked<U: $trait>(&self) -> &U {
&*(self as *const Self as *const U)
}
#[inline]
pub fn is<U: $trait>(&self) -> bool {
use std::any::TypeId;
TypeId::of::<U>() == $crate::anyimpl::AnyImpl::get_type_id(self)
}
}
};
}

View file

@ -8,7 +8,6 @@
use std::ptr; use std::ptr;
use std::mem; use std::mem;
use mopa;
use glib_ffi; use glib_ffi;
use gobject_ffi; use gobject_ffi;
@ -24,9 +23,10 @@ use gst_base::prelude::*;
use object::*; use object::*;
use element::*; use element::*;
use anyimpl::*;
pub trait BaseSinkImpl<T: BaseSink> pub trait BaseSinkImpl<T: BaseSink>
: mopa::Any + ObjectImpl<T> + ElementImpl<T> + Send + Sync + 'static { : AnyImpl + ObjectImpl<T> + ElementImpl<T> + Send + Sync + 'static {
fn start(&self, _element: &T) -> bool { fn start(&self, _element: &T) -> bool {
true true
} }
@ -92,7 +92,7 @@ pub trait BaseSinkImpl<T: BaseSink>
} }
} }
mopafy_object_impl!(BaseSink, BaseSinkImpl); any_impl!(BaseSink, BaseSinkImpl);
pub unsafe trait BaseSink pub unsafe trait BaseSink
: IsA<gst::Element> + IsA<gst_base::BaseSink> + ObjectType { : IsA<gst::Element> + IsA<gst_base::BaseSink> + ObjectType {

View file

@ -8,7 +8,6 @@
use std::ptr; use std::ptr;
use std::mem; use std::mem;
use mopa;
use glib_ffi; use glib_ffi;
use gobject_ffi; use gobject_ffi;
@ -24,9 +23,10 @@ use gst_base::prelude::*;
use object::*; use object::*;
use element::*; use element::*;
use anyimpl::*;
pub trait BaseSrcImpl<T: BaseSrc> pub trait BaseSrcImpl<T: BaseSrc>
: mopa::Any + ObjectImpl<T> + ElementImpl<T> + Send + Sync + 'static { : AnyImpl + ObjectImpl<T> + ElementImpl<T> + Send + Sync + 'static {
fn start(&self, _element: &T) -> bool { fn start(&self, _element: &T) -> bool {
true true
} }
@ -99,7 +99,7 @@ pub trait BaseSrcImpl<T: BaseSrc>
} }
} }
mopafy_object_impl!(BaseSrc, BaseSrcImpl); any_impl!(BaseSrc, BaseSrcImpl);
pub unsafe trait BaseSrc pub unsafe trait BaseSrc
: IsA<gst::Element> + IsA<gst_base::BaseSrc> + ObjectType { : IsA<gst::Element> + IsA<gst_base::BaseSrc> + ObjectType {

View file

@ -8,7 +8,6 @@
use std::ptr; use std::ptr;
use std::mem; use std::mem;
use mopa;
use libc; use libc;
@ -22,9 +21,10 @@ use gst;
use gst::prelude::*; use gst::prelude::*;
use object::*; use object::*;
use anyimpl::*;
pub trait ElementImpl<T: Element> pub trait ElementImpl<T: Element>
: ObjectImpl<T> + mopa::Any + Send + Sync + 'static { : ObjectImpl<T> + AnyImpl + Send + Sync + 'static {
fn change_state(&self, element: &T, transition: gst::StateChange) -> gst::StateChangeReturn { fn change_state(&self, element: &T, transition: gst::StateChange) -> gst::StateChangeReturn {
element.parent_change_state(transition) element.parent_change_state(transition)
} }
@ -54,7 +54,7 @@ pub trait ElementImpl<T: Element>
} }
} }
mopafy_object_impl!(Element, ElementImpl); any_impl!(Element, ElementImpl);
pub unsafe trait Element: IsA<gst::Element> + ObjectType { pub unsafe trait Element: IsA<gst::Element> + ObjectType {
fn parent_change_state(&self, transition: gst::StateChange) -> gst::StateChangeReturn { fn parent_change_state(&self, transition: gst::StateChange) -> gst::StateChangeReturn {

View file

@ -12,7 +12,6 @@ extern crate gstreamer_base_sys as gst_base_ffi;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
extern crate libc; extern crate libc;
extern crate mopa;
extern crate url; extern crate url;
pub extern crate glib_sys as glib_ffi; pub extern crate glib_sys as glib_ffi;
pub extern crate gobject_sys as gobject_ffi; pub extern crate gobject_sys as gobject_ffi;
@ -57,35 +56,8 @@ impl Drop for FloatingReferenceGuard {
} }
} }
// mopafy! macro to work with generic traits over T: ObjectType #[macro_use]
macro_rules! mopafy_object_impl { pub mod anyimpl;
($bound:ident, $trait:ident) => {
impl<T: $bound> $trait<T> {
#[inline]
pub fn downcast_ref<U: $trait<T>>(&self) -> Option<&U> {
if self.is::<U>() {
unsafe {
Some(self.downcast_ref_unchecked())
}
} else {
None
}
}
#[inline]
pub unsafe fn downcast_ref_unchecked<U: $trait<T>>(&self) -> &U {
&*(self as *const Self as *const U)
}
#[inline]
pub fn is<U: $trait<T>>(&self) -> bool {
use std::any::TypeId;
use mopa;
TypeId::of::<U>() == mopa::Any::get_type_id(self)
}
}
};
}
#[macro_use] #[macro_use]
pub mod utils; pub mod utils;

View file

@ -7,8 +7,6 @@
// except according to those terms. // except according to those terms.
use std::ptr; use std::ptr;
use mopa;
use glib_ffi; use glib_ffi;
use gobject_ffi; use gobject_ffi;
use gst_ffi; use gst_ffi;
@ -20,12 +18,15 @@ use gst;
use gst::prelude::*; use gst::prelude::*;
use object::*; use object::*;
use anyimpl::*;
pub trait URIHandlerImpl: mopa::Any + Send + Sync + 'static { pub trait URIHandlerImpl: AnyImpl + Send + Sync + 'static {
fn get_uri(&self, element: &gst::URIHandler) -> Option<String>; fn get_uri(&self, element: &gst::URIHandler) -> Option<String>;
fn set_uri(&self, element: &gst::URIHandler, uri: Option<String>) -> Result<(), glib::Error>; fn set_uri(&self, element: &gst::URIHandler, uri: Option<String>) -> Result<(), glib::Error>;
} }
any_impl!(URIHandlerImpl);
pub trait URIHandlerImplStatic<T: ObjectType>: Send + Sync + 'static { pub trait URIHandlerImplStatic<T: ObjectType>: Send + Sync + 'static {
fn get_impl<'a>(&self, imp: &'a T::ImplType) -> &'a URIHandlerImpl; fn get_impl<'a>(&self, imp: &'a T::ImplType) -> &'a URIHandlerImpl;
fn get_type(&self) -> gst::URIType; fn get_type(&self) -> gst::URIType;