diff --git a/gst-plugin/Cargo.toml b/gst-plugin/Cargo.toml index 28f66a79..45577b3d 100644 --- a/gst-plugin/Cargo.toml +++ b/gst-plugin/Cargo.toml @@ -10,7 +10,6 @@ libc = "0.2" url = "1.1" lazy_static = "0.2" byteorder = "1.0" -mopa = "0.2" glib-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"] } diff --git a/gst-plugin/src/anyimpl.rs b/gst-plugin/src/anyimpl.rs new file mode 100644 index 00000000..2415ea4b --- /dev/null +++ b/gst-plugin/src/anyimpl.rs @@ -0,0 +1,75 @@ +// Copyright (C) 2017 Sebastian Dröge +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , 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 AnyImpl for T { + fn get_type_id(&self) -> TypeId { + TypeId::of::() + } +} + +macro_rules! any_impl { + ($bound:ident, $trait:ident) => { + impl $trait { + #[inline] + pub fn downcast_ref>(&self) -> Option<&U> { + if self.is::() { + unsafe { + Some(self.downcast_ref_unchecked()) + } + } else { + None + } + } + + #[inline] + pub unsafe fn downcast_ref_unchecked>(&self) -> &U { + &*(self as *const Self as *const U) + } + + #[inline] + pub fn is>(&self) -> bool { + use std::any::TypeId; + TypeId::of::() == $crate::anyimpl::AnyImpl::get_type_id(self) + } + } + }; + + ($trait:ident) => { + impl $trait { + #[inline] + pub fn downcast_ref(&self) -> Option<&U> { + if self.is::() { + unsafe { + Some(self.downcast_ref_unchecked()) + } + } else { + None + } + } + + #[inline] + pub unsafe fn downcast_ref_unchecked(&self) -> &U { + &*(self as *const Self as *const U) + } + + #[inline] + pub fn is(&self) -> bool { + use std::any::TypeId; + TypeId::of::() == $crate::anyimpl::AnyImpl::get_type_id(self) + } + } + }; +} diff --git a/gst-plugin/src/base_sink.rs b/gst-plugin/src/base_sink.rs index 338606b8..a3da75cd 100644 --- a/gst-plugin/src/base_sink.rs +++ b/gst-plugin/src/base_sink.rs @@ -8,7 +8,6 @@ use std::ptr; use std::mem; -use mopa; use glib_ffi; use gobject_ffi; @@ -24,9 +23,10 @@ use gst_base::prelude::*; use object::*; use element::*; +use anyimpl::*; pub trait BaseSinkImpl - : mopa::Any + ObjectImpl + ElementImpl + Send + Sync + 'static { + : AnyImpl + ObjectImpl + ElementImpl + Send + Sync + 'static { fn start(&self, _element: &T) -> bool { true } @@ -92,7 +92,7 @@ pub trait BaseSinkImpl } } -mopafy_object_impl!(BaseSink, BaseSinkImpl); +any_impl!(BaseSink, BaseSinkImpl); pub unsafe trait BaseSink : IsA + IsA + ObjectType { diff --git a/gst-plugin/src/base_src.rs b/gst-plugin/src/base_src.rs index 60bba88b..43fca18d 100644 --- a/gst-plugin/src/base_src.rs +++ b/gst-plugin/src/base_src.rs @@ -8,7 +8,6 @@ use std::ptr; use std::mem; -use mopa; use glib_ffi; use gobject_ffi; @@ -24,9 +23,10 @@ use gst_base::prelude::*; use object::*; use element::*; +use anyimpl::*; pub trait BaseSrcImpl - : mopa::Any + ObjectImpl + ElementImpl + Send + Sync + 'static { + : AnyImpl + ObjectImpl + ElementImpl + Send + Sync + 'static { fn start(&self, _element: &T) -> bool { true } @@ -99,7 +99,7 @@ pub trait BaseSrcImpl } } -mopafy_object_impl!(BaseSrc, BaseSrcImpl); +any_impl!(BaseSrc, BaseSrcImpl); pub unsafe trait BaseSrc : IsA + IsA + ObjectType { diff --git a/gst-plugin/src/element.rs b/gst-plugin/src/element.rs index 35a65df3..20d3871b 100644 --- a/gst-plugin/src/element.rs +++ b/gst-plugin/src/element.rs @@ -8,7 +8,6 @@ use std::ptr; use std::mem; -use mopa; use libc; @@ -22,9 +21,10 @@ use gst; use gst::prelude::*; use object::*; +use anyimpl::*; pub trait ElementImpl - : ObjectImpl + mopa::Any + Send + Sync + 'static { + : ObjectImpl + AnyImpl + Send + Sync + 'static { fn change_state(&self, element: &T, transition: gst::StateChange) -> gst::StateChangeReturn { element.parent_change_state(transition) } @@ -54,7 +54,7 @@ pub trait ElementImpl } } -mopafy_object_impl!(Element, ElementImpl); +any_impl!(Element, ElementImpl); pub unsafe trait Element: IsA + ObjectType { fn parent_change_state(&self, transition: gst::StateChange) -> gst::StateChangeReturn { diff --git a/gst-plugin/src/lib.rs b/gst-plugin/src/lib.rs index b323e302..ce4659bf 100644 --- a/gst-plugin/src/lib.rs +++ b/gst-plugin/src/lib.rs @@ -12,7 +12,6 @@ extern crate gstreamer_base_sys as gst_base_ffi; #[macro_use] extern crate lazy_static; extern crate libc; -extern crate mopa; extern crate url; pub extern crate glib_sys as glib_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_rules! mopafy_object_impl { - ($bound:ident, $trait:ident) => { - impl $trait { - #[inline] - pub fn downcast_ref>(&self) -> Option<&U> { - if self.is::() { - unsafe { - Some(self.downcast_ref_unchecked()) - } - } else { - None - } - } - - #[inline] - pub unsafe fn downcast_ref_unchecked>(&self) -> &U { - &*(self as *const Self as *const U) - } - - #[inline] - pub fn is>(&self) -> bool { - use std::any::TypeId; - use mopa; - TypeId::of::() == mopa::Any::get_type_id(self) - } - } - }; -} +#[macro_use] +pub mod anyimpl; #[macro_use] pub mod utils; diff --git a/gst-plugin/src/uri_handler.rs b/gst-plugin/src/uri_handler.rs index 88af4047..89d20add 100644 --- a/gst-plugin/src/uri_handler.rs +++ b/gst-plugin/src/uri_handler.rs @@ -7,8 +7,6 @@ // except according to those terms. use std::ptr; -use mopa; - use glib_ffi; use gobject_ffi; use gst_ffi; @@ -20,12 +18,15 @@ use gst; use gst::prelude::*; 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; fn set_uri(&self, element: &gst::URIHandler, uri: Option) -> Result<(), glib::Error>; } +any_impl!(URIHandlerImpl); + pub trait URIHandlerImplStatic: Send + Sync + 'static { fn get_impl<'a>(&self, imp: &'a T::ImplType) -> &'a URIHandlerImpl; fn get_type(&self) -> gst::URIType;