From af01f1bc677be2037d34c8e49f598a8deee68d3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Tue, 30 Jun 2020 00:12:46 +0300 Subject: [PATCH] gstreamer: Simplify MiniObject bindings by removing one layer of abstraction And instead directly implementing this via the macro on the target types. --- examples/src/bin/custom_meta.rs | 1 - gstreamer-audio/src/audio_buffer.rs | 1 - gstreamer-audio/src/audio_channel_position.rs | 1 - gstreamer-audio/src/subclass/audio_decoder.rs | 1 - gstreamer-audio/src/subclass/audio_encoder.rs | 1 - gstreamer-base/src/base_parse_frame.rs | 1 - gstreamer-base/src/subclass/aggregator.rs | 5 +- gstreamer-base/src/subclass/base_sink.rs | 5 +- gstreamer-base/src/subclass/base_src.rs | 5 +- gstreamer-base/src/subclass/push_src.rs | 5 +- gstreamer-gl/src/context.rs | 2 +- gstreamer-gl/src/gl_video_frame.rs | 1 - gstreamer-pbutils/src/functions.rs | 1 - gstreamer-rtp/src/rtp_buffer.rs | 1 - gstreamer-rtsp-server/src/rtsp_thread.rs | 4 +- gstreamer-rtsp-server/src/rtsp_token.rs | 8 +- .../src/subclass/rtsp_media.rs | 4 +- gstreamer-sdp/src/sdp_media.rs | 2 +- gstreamer-sdp/src/sdp_message.rs | 1 - gstreamer-video/src/subclass/video_decoder.rs | 1 - gstreamer-video/src/subclass/video_encoder.rs | 1 - gstreamer-video/src/video_codec_frame.rs | 1 - gstreamer-video/src/video_codec_state.rs | 1 - gstreamer-video/src/video_event.rs | 1 - gstreamer-video/src/video_frame.rs | 1 - gstreamer-video/src/video_overlay.rs | 1 - .../src/video_overlay_composition.rs | 15 +- gstreamer/src/buffer.rs | 25 +- gstreamer/src/buffer_cursor.rs | 1 - gstreamer/src/buffer_serde.rs | 16 +- gstreamer/src/bufferlist.rs | 17 +- gstreamer/src/caps.rs | 37 +- gstreamer/src/context.rs | 9 +- gstreamer/src/element.rs | 1 - gstreamer/src/event.rs | 9 +- gstreamer/src/lib.rs | 2 - gstreamer/src/memory.rs | 10 +- gstreamer/src/message.rs | 9 +- gstreamer/src/meta.rs | 1 - gstreamer/src/miniobject.rs | 690 ++++-------------- gstreamer/src/pad.rs | 1 - gstreamer/src/query.rs | 19 +- gstreamer/src/sample.rs | 9 +- gstreamer/src/tags.rs | 38 +- gstreamer/src/toc.rs | 25 +- 45 files changed, 321 insertions(+), 670 deletions(-) diff --git a/examples/src/bin/custom_meta.rs b/examples/src/bin/custom_meta.rs index 03297c56c..7894868b5 100644 --- a/examples/src/bin/custom_meta.rs +++ b/examples/src/bin/custom_meta.rs @@ -78,7 +78,6 @@ mod custom_meta { use glib::glib_sys; use glib::translate::*; use gst::gst_sys; - use gst::prelude::*; use once_cell::sync::Lazy; use std::mem; use std::ptr; diff --git a/gstreamer-audio/src/audio_buffer.rs b/gstreamer-audio/src/audio_buffer.rs index d1b8f3d72..557ae72ec 100644 --- a/gstreamer-audio/src/audio_buffer.rs +++ b/gstreamer-audio/src/audio_buffer.rs @@ -12,7 +12,6 @@ use gst_sys; use glib; use glib::translate::{from_glib, Borrowed, FromGlibPtrNone, ToGlibPtr}; use gst; -use gst::miniobject::MiniObject; use std::fmt; use std::marker::PhantomData; diff --git a/gstreamer-audio/src/audio_channel_position.rs b/gstreamer-audio/src/audio_channel_position.rs index d004960cb..c97141da6 100644 --- a/gstreamer-audio/src/audio_channel_position.rs +++ b/gstreamer-audio/src/audio_channel_position.rs @@ -14,7 +14,6 @@ use std::mem; use glib; use glib::translate::{from_glib, ToGlib}; use gst; -use gst::MiniObject; use array_init; diff --git a/gstreamer-audio/src/subclass/audio_decoder.rs b/gstreamer-audio/src/subclass/audio_decoder.rs index d85c9fe34..e30ab1b0a 100644 --- a/gstreamer-audio/src/subclass/audio_decoder.rs +++ b/gstreamer-audio/src/subclass/audio_decoder.rs @@ -15,7 +15,6 @@ use glib::translate::*; use glib::subclass::prelude::*; use gst; use gst::subclass::prelude::*; -use gst::MiniObject; use gst_base; use std::mem; diff --git a/gstreamer-audio/src/subclass/audio_encoder.rs b/gstreamer-audio/src/subclass/audio_encoder.rs index 1092ff6a8..5e8a28ac4 100644 --- a/gstreamer-audio/src/subclass/audio_encoder.rs +++ b/gstreamer-audio/src/subclass/audio_encoder.rs @@ -15,7 +15,6 @@ use glib::translate::*; use glib::subclass::prelude::*; use gst; use gst::subclass::prelude::*; -use gst::MiniObject; use std::ptr; diff --git a/gstreamer-base/src/base_parse_frame.rs b/gstreamer-base/src/base_parse_frame.rs index 9d81902a7..c568a0e84 100644 --- a/gstreamer-base/src/base_parse_frame.rs +++ b/gstreamer-base/src/base_parse_frame.rs @@ -8,7 +8,6 @@ use glib::translate::*; use gst; -use gst::MiniObject; use gst_base_sys; use std::convert::TryFrom; use std::fmt; diff --git a/gstreamer-base/src/subclass/aggregator.rs b/gstreamer-base/src/subclass/aggregator.rs index f6f38f9fa..4dbbd19b5 100644 --- a/gstreamer-base/src/subclass/aggregator.rs +++ b/gstreamer-base/src/subclass/aggregator.rs @@ -12,10 +12,9 @@ use glib_sys; use gst_base_sys; use gst_sys; -use glib::translate::*; -use prelude::*; - use glib::subclass::prelude::*; +use glib::translate::*; + use gst; use gst::subclass::prelude::*; diff --git a/gstreamer-base/src/subclass/base_sink.rs b/gstreamer-base/src/subclass/base_sink.rs index 3cce9d85e..7dc2ea4c0 100644 --- a/gstreamer-base/src/subclass/base_sink.rs +++ b/gstreamer-base/src/subclass/base_sink.rs @@ -10,10 +10,9 @@ use glib_sys; use gst_base_sys; use gst_sys; -use glib::translate::*; -use prelude::*; - use glib::subclass::prelude::*; +use glib::translate::*; + use gst; use gst::subclass::prelude::*; diff --git a/gstreamer-base/src/subclass/base_src.rs b/gstreamer-base/src/subclass/base_src.rs index 1bf4195be..5f212f6d0 100644 --- a/gstreamer-base/src/subclass/base_src.rs +++ b/gstreamer-base/src/subclass/base_src.rs @@ -10,10 +10,9 @@ use glib_sys; use gst_base_sys; use gst_sys; -use glib::translate::*; -use prelude::*; - use glib::subclass::prelude::*; +use glib::translate::*; + use gst; use gst::subclass::prelude::*; diff --git a/gstreamer-base/src/subclass/push_src.rs b/gstreamer-base/src/subclass/push_src.rs index 203ab93da..9dbb117b8 100644 --- a/gstreamer-base/src/subclass/push_src.rs +++ b/gstreamer-base/src/subclass/push_src.rs @@ -9,10 +9,9 @@ use gst_base_sys; use gst_sys; -use glib::translate::*; -use prelude::*; - use glib::subclass::prelude::*; +use glib::translate::*; + use gst; use gst::subclass::prelude::*; diff --git a/gstreamer-gl/src/context.rs b/gstreamer-gl/src/context.rs index 167aebbcd..47dea5d32 100644 --- a/gstreamer-gl/src/context.rs +++ b/gstreamer-gl/src/context.rs @@ -8,7 +8,7 @@ use glib::translate::*; use glib::IsA; -use gst::{ContextRef, MiniObject}; +use gst::ContextRef; use gst_gl_sys; use std::ptr; use GLDisplay; diff --git a/gstreamer-gl/src/gl_video_frame.rs b/gstreamer-gl/src/gl_video_frame.rs index e9646a70a..912222e8d 100644 --- a/gstreamer-gl/src/gl_video_frame.rs +++ b/gstreamer-gl/src/gl_video_frame.rs @@ -13,7 +13,6 @@ use gst_video_sys; use glib::translate::{from_glib, ToGlibPtr}; use gst; -use gst::MiniObject; use gst_video::video_frame::Readable; use gst_video::*; diff --git a/gstreamer-pbutils/src/functions.rs b/gstreamer-pbutils/src/functions.rs index 26ccea182..6d0b33755 100644 --- a/gstreamer-pbutils/src/functions.rs +++ b/gstreamer-pbutils/src/functions.rs @@ -9,7 +9,6 @@ use glib; use glib::translate::*; use gst; -use gst::MiniObject; use gst_pbutils_sys; use std::ptr; diff --git a/gstreamer-rtp/src/rtp_buffer.rs b/gstreamer-rtp/src/rtp_buffer.rs index 4ff220b6e..2f9629d9b 100644 --- a/gstreamer-rtp/src/rtp_buffer.rs +++ b/gstreamer-rtp/src/rtp_buffer.rs @@ -5,7 +5,6 @@ use std::mem; use std::ptr; use std::slice; -use gst::MiniObject; use gst_rtp_sys; pub enum Readable {} diff --git a/gstreamer-rtsp-server/src/rtsp_thread.rs b/gstreamer-rtsp-server/src/rtsp_thread.rs index 6ace67caf..4f084c79f 100644 --- a/gstreamer-rtsp-server/src/rtsp_thread.rs +++ b/gstreamer-rtsp-server/src/rtsp_thread.rs @@ -1,14 +1,12 @@ use glib; use glib::translate::*; -use gst_rtsp_server_sys; -use gst::prelude::*; +use gst_rtsp_server_sys; gst_define_mini_object_wrapper!( RTSPThread, RTSPThreadRef, gst_rtsp_server_sys::GstRTSPThread, - [], || gst_rtsp_server_sys::gst_rtsp_thread_get_type() ); diff --git a/gstreamer-rtsp-server/src/rtsp_token.rs b/gstreamer-rtsp-server/src/rtsp_token.rs index e76d604b3..72521b063 100644 --- a/gstreamer-rtsp-server/src/rtsp_token.rs +++ b/gstreamer-rtsp-server/src/rtsp_token.rs @@ -2,7 +2,6 @@ use glib; use glib::translate::*; use glib::value::ToSendValue; use gst; -use gst::miniobject::*; use gst_rtsp_server_sys; use std::fmt; @@ -11,7 +10,6 @@ gst_define_mini_object_wrapper!( RTSPToken, RTSPTokenRef, gst_rtsp_server_sys::GstRTSPToken, - [Debug,], || gst_rtsp_server_sys::gst_rtsp_token_get_type() ); @@ -78,6 +76,12 @@ impl RTSPTokenRef { } } +impl fmt::Debug for RTSPToken { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + RTSPTokenRef::fmt(self, f) + } +} + impl fmt::Debug for RTSPTokenRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("RTSPToken") diff --git a/gstreamer-rtsp-server/src/subclass/rtsp_media.rs b/gstreamer-rtsp-server/src/subclass/rtsp_media.rs index a9ea2938d..8384153a8 100644 --- a/gstreamer-rtsp-server/src/subclass/rtsp_media.rs +++ b/gstreamer-rtsp-server/src/subclass/rtsp_media.rs @@ -8,11 +8,9 @@ use gst_rtsp_server_sys; +use glib::subclass::prelude::*; use glib::translate::*; -use glib::subclass::prelude::*; - -use gst::prelude::*; use std::ptr; use RTSPMedia; diff --git a/gstreamer-sdp/src/sdp_media.rs b/gstreamer-sdp/src/sdp_media.rs index e7a3f563d..c8200d976 100644 --- a/gstreamer-sdp/src/sdp_media.rs +++ b/gstreamer-sdp/src/sdp_media.rs @@ -15,7 +15,7 @@ use std::ptr; use glib::translate::*; use gst; -use gst::prelude::*; + use gst_sdp_sys; use sdp_attribute::SDPAttribute; diff --git a/gstreamer-sdp/src/sdp_message.rs b/gstreamer-sdp/src/sdp_message.rs index a92b3961e..d8a2b20fb 100644 --- a/gstreamer-sdp/src/sdp_message.rs +++ b/gstreamer-sdp/src/sdp_message.rs @@ -16,7 +16,6 @@ use std::ptr; use glib::translate::*; use gobject_sys; use gst; -use gst::MiniObject; use gst_sdp_sys; use sdp_attribute::SDPAttribute; diff --git a/gstreamer-video/src/subclass/video_decoder.rs b/gstreamer-video/src/subclass/video_decoder.rs index 08c573ebc..5d8d436c0 100644 --- a/gstreamer-video/src/subclass/video_decoder.rs +++ b/gstreamer-video/src/subclass/video_decoder.rs @@ -16,7 +16,6 @@ use glib::translate::*; use glib::subclass::prelude::*; use gst; use gst::subclass::prelude::*; -use gst::MiniObject; use gst_base; use crate::prelude::*; diff --git a/gstreamer-video/src/subclass/video_encoder.rs b/gstreamer-video/src/subclass/video_encoder.rs index b7fd1b356..849633657 100644 --- a/gstreamer-video/src/subclass/video_encoder.rs +++ b/gstreamer-video/src/subclass/video_encoder.rs @@ -16,7 +16,6 @@ use glib::translate::*; use glib::subclass::prelude::*; use gst; use gst::subclass::prelude::*; -use gst::MiniObject; use crate::prelude::*; use video_codec_state::{Readable, VideoCodecState}; diff --git a/gstreamer-video/src/video_codec_frame.rs b/gstreamer-video/src/video_codec_frame.rs index 72a99e3ce..3e9401d55 100644 --- a/gstreamer-video/src/video_codec_frame.rs +++ b/gstreamer-video/src/video_codec_frame.rs @@ -9,7 +9,6 @@ use glib::translate::*; use gst; -use gst::miniobject::MiniObject; use gst_video_sys; use std::fmt; use std::mem; diff --git a/gstreamer-video/src/video_codec_state.rs b/gstreamer-video/src/video_codec_state.rs index 278458149..ba7087237 100644 --- a/gstreamer-video/src/video_codec_state.rs +++ b/gstreamer-video/src/video_codec_state.rs @@ -16,7 +16,6 @@ use std::ptr; use utils::HasStreamLock; use gst; -use gst::miniobject::MiniObject; use video_info::VideoInfo; diff --git a/gstreamer-video/src/video_event.rs b/gstreamer-video/src/video_event.rs index f4e80801f..32d21afe9 100644 --- a/gstreamer-video/src/video_event.rs +++ b/gstreamer-video/src/video_event.rs @@ -12,7 +12,6 @@ use gst_video_sys; use glib::translate::{from_glib, from_glib_full, ToGlib}; use glib::ToSendValue; use gst; -use gst::MiniObject; use std::mem; // FIXME: Copy from gstreamer/src/event.rs diff --git a/gstreamer-video/src/video_frame.rs b/gstreamer-video/src/video_frame.rs index 6ecc14b59..f2ff4e369 100644 --- a/gstreamer-video/src/video_frame.rs +++ b/gstreamer-video/src/video_frame.rs @@ -12,7 +12,6 @@ use gst_video_sys; use glib; use glib::translate::{from_glib, Borrowed, ToGlibPtr}; use gst; -use gst::miniobject::MiniObject; use std::fmt; use std::marker::PhantomData; diff --git a/gstreamer-video/src/video_overlay.rs b/gstreamer-video/src/video_overlay.rs index a054795ed..dffc722a6 100644 --- a/gstreamer-video/src/video_overlay.rs +++ b/gstreamer-video/src/video_overlay.rs @@ -8,7 +8,6 @@ use glib::translate::*; use gst; -use gst::prelude::*; use gst_video_sys; use libc::uintptr_t; use VideoOverlay; diff --git a/gstreamer-video/src/video_overlay_composition.rs b/gstreamer-video/src/video_overlay_composition.rs index 00b2d4a31..8e5db54d7 100644 --- a/gstreamer-video/src/video_overlay_composition.rs +++ b/gstreamer-video/src/video_overlay_composition.rs @@ -10,7 +10,6 @@ use std::fmt; use std::mem; use gst; -use gst::miniobject::*; use gst_video_sys; use glib; @@ -20,10 +19,15 @@ gst_define_mini_object_wrapper!( VideoOverlayRectangle, VideoOverlayRectangleRef, gst_video_sys::GstVideoOverlayRectangle, - [Debug,], || gst_video_sys::gst_video_overlay_rectangle_get_type() ); +impl fmt::Debug for VideoOverlayRectangle { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + VideoOverlayRectangleRef::fmt(self, f) + } +} + impl fmt::Debug for VideoOverlayRectangleRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("VideoOverlayRectangle") @@ -188,10 +192,15 @@ gst_define_mini_object_wrapper!( VideoOverlayComposition, VideoOverlayCompositionRef, gst_video_sys::GstVideoOverlayComposition, - [Debug,], || gst_video_sys::gst_video_overlay_composition_get_type() ); +impl fmt::Debug for VideoOverlayComposition { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + VideoOverlayCompositionRef::fmt(self, f) + } +} + impl fmt::Debug for VideoOverlayCompositionRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("VideoOverlayComposition").finish() diff --git a/gstreamer/src/buffer.rs b/gstreamer/src/buffer.rs index 136e441e8..dd05e1f37 100644 --- a/gstreamer/src/buffer.rs +++ b/gstreamer/src/buffer.rs @@ -16,7 +16,6 @@ use std::u64; use std::usize; use meta::*; -use miniobject::*; use BufferCursor; use BufferFlags; use BufferRefCursor; @@ -32,13 +31,9 @@ use gst_sys; pub enum Readable {} pub enum Writable {} -gst_define_mini_object_wrapper!( - Buffer, - BufferRef, - gst_sys::GstBuffer, - [Debug, PartialEq, Eq,], - || gst_sys::gst_buffer_get_type() -); +gst_define_mini_object_wrapper!(Buffer, BufferRef, gst_sys::GstBuffer, || { + gst_sys::gst_buffer_get_type() +}); pub struct BufferMap<'a, T> { buffer: &'a BufferRef, @@ -832,6 +827,20 @@ define_iter!( |buffer: &BufferRef, idx| { buffer.get_memory(idx) } ); +impl fmt::Debug for Buffer { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + BufferRef::fmt(self, f) + } +} + +impl PartialEq for Buffer { + fn eq(&self, other: &Buffer) -> bool { + BufferRef::eq(self, other) + } +} + +impl Eq for Buffer {} + impl fmt::Debug for BufferRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use std::cell::RefCell; diff --git a/gstreamer/src/buffer_cursor.rs b/gstreamer/src/buffer_cursor.rs index fa32184d7..83147edc3 100644 --- a/gstreamer/src/buffer_cursor.rs +++ b/gstreamer/src/buffer_cursor.rs @@ -14,7 +14,6 @@ use std::ptr; use std::u64; use std::usize; -use miniobject::*; use Buffer; use BufferRef; diff --git a/gstreamer/src/buffer_serde.rs b/gstreamer/src/buffer_serde.rs index 16b2a49b0..1a199261f 100644 --- a/gstreamer/src/buffer_serde.rs +++ b/gstreamer/src/buffer_serde.rs @@ -98,7 +98,7 @@ mod tests { buffer.set_offset(3); buffer.set_offset_end(4); buffer.set_duration(5.into()); - buffer.set_flags(BufferFlags::LIVE | BufferFlags::LAST); + buffer.set_flags(BufferFlags::LIVE | BufferFlags::DISCONT); } let mut pretty_config = ron::ser::PrettyConfig::default(); @@ -114,7 +114,7 @@ mod tests { " offset: 3,", " offset_end: 4,", " flags: (", - " bits: 1048592,", + " bits: 80,", " ),", " buffer: \"AQIDBA==\",", ")" @@ -132,7 +132,7 @@ mod tests { "\"duration\":5,", "\"offset\":3,", "\"offset_end\":4,", - "\"flags\":{\"bits\":1048592},", + "\"flags\":{\"bits\":80},", "\"buffer\":[1,2,3,4]", "}" ) @@ -153,7 +153,7 @@ mod tests { offset: 3, offset_end: 4, flags: ( - bits: 1048592, + bits: 80, ), buffer: "AQIDBA==", ) @@ -164,7 +164,7 @@ mod tests { assert_eq!(buffer.get_offset(), 3); assert_eq!(buffer.get_offset_end(), 4); assert_eq!(buffer.get_duration(), 5.into()); - assert_eq!(buffer.get_flags(), BufferFlags::LIVE | BufferFlags::LAST); + assert_eq!(buffer.get_flags(), BufferFlags::LIVE | BufferFlags::DISCONT); { let data = buffer.map_readable().unwrap(); assert_eq!(data.as_slice(), vec![1, 2, 3, 4].as_slice()); @@ -177,7 +177,7 @@ mod tests { "duration":5, "offset":3, "offset_end":4, - "flags":{"bits":1048592}, + "flags":{"bits":80}, "buffer":[1,2,3,4] } "#; @@ -187,7 +187,7 @@ mod tests { assert_eq!(buffer.get_offset(), 3); assert_eq!(buffer.get_offset_end(), 4); assert_eq!(buffer.get_duration(), 5.into()); - assert_eq!(buffer.get_flags(), BufferFlags::LIVE | BufferFlags::LAST); + assert_eq!(buffer.get_flags(), BufferFlags::LIVE | BufferFlags::DISCONT); { let data = buffer.map_readable().unwrap(); assert_eq!(data.as_slice(), vec![1, 2, 3, 4].as_slice()); @@ -205,7 +205,7 @@ mod tests { buffer.set_offset(3); buffer.set_offset_end(4); buffer.set_duration(5.into()); - buffer.set_flags(BufferFlags::LIVE | BufferFlags::LAST); + buffer.set_flags(BufferFlags::LIVE | BufferFlags::DISCONT); } // Ron diff --git a/gstreamer/src/bufferlist.rs b/gstreamer/src/bufferlist.rs index 112a345fb..e10525474 100644 --- a/gstreamer/src/bufferlist.rs +++ b/gstreamer/src/bufferlist.rs @@ -11,17 +11,12 @@ use glib::translate::{from_glib_full, from_glib_none}; use gst_sys; use std::fmt; -use miniobject::*; use Buffer; use BufferRef; -gst_define_mini_object_wrapper!( - BufferList, - BufferListRef, - gst_sys::GstBufferList, - [Debug,], - || gst_sys::gst_buffer_list_get_type() -); +gst_define_mini_object_wrapper!(BufferList, BufferListRef, gst_sys::GstBufferList, || { + gst_sys::gst_buffer_list_get_type() +}); impl BufferList { pub fn new() -> Self { @@ -112,6 +107,12 @@ impl Default for BufferList { } } +impl fmt::Debug for BufferList { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + BufferListRef::fmt(self, f) + } +} + impl fmt::Debug for BufferListRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let size = self.iter().map(|b| b.get_size()).sum::(); diff --git a/gstreamer/src/caps.rs b/gstreamer/src/caps.rs index 2b25f53aa..c1f5ced67 100644 --- a/gstreamer/src/caps.rs +++ b/gstreamer/src/caps.rs @@ -7,7 +7,6 @@ // except according to those terms. use caps_features::*; -use miniobject::*; use std::fmt; use std::marker::PhantomData; use std::ptr; @@ -21,13 +20,9 @@ use glib::translate::{from_glib, from_glib_full, FromGlibPtrFull, ToGlib, ToGlib use glib::value::ToSendValue; use gst_sys; -gst_define_mini_object_wrapper!( - Caps, - CapsRef, - gst_sys::GstCaps, - [Debug, PartialEq, Eq,], - || gst_sys::gst_caps_get_type() -); +gst_define_mini_object_wrapper!(Caps, CapsRef, gst_sys::GstCaps, || { + gst_sys::gst_caps_get_type() +}); impl Caps { pub fn builder(name: &str) -> Builder { @@ -179,12 +174,6 @@ impl str::FromStr for Caps { } } -impl fmt::Display for Caps { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.0.fmt(f) - } -} - impl CapsRef { pub fn set_simple(&mut self, values: &[(&str, &dyn ToSendValue)]) { for &(name, value) in values { @@ -557,6 +546,26 @@ define_iter!( } ); +impl fmt::Debug for Caps { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + ::fmt(self, f) + } +} + +impl fmt::Display for Caps { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + ::fmt(self, f) + } +} + +impl PartialEq for Caps { + fn eq(&self, other: &Caps) -> bool { + CapsRef::eq(self, other) + } +} + +impl Eq for Caps {} + impl fmt::Debug for CapsRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_tuple("Caps").field(&self.to_string()).finish() diff --git a/gstreamer/src/context.rs b/gstreamer/src/context.rs index 6e158a45e..cd8b63ba6 100644 --- a/gstreamer/src/context.rs +++ b/gstreamer/src/context.rs @@ -14,10 +14,9 @@ use gst_sys; use glib; use glib::translate::{from_glib, from_glib_full, ToGlib, ToGlibPtr}; -use miniobject::*; use StructureRef; -gst_define_mini_object_wrapper!(Context, ContextRef, gst_sys::GstContext, [Debug,], || { +gst_define_mini_object_wrapper!(Context, ContextRef, gst_sys::GstContext, || { gst_sys::gst_context_get_type() }); @@ -69,6 +68,12 @@ impl ContextRef { } } +impl fmt::Debug for Context { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + ContextRef::fmt(self, f) + } +} + impl fmt::Debug for ContextRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Context") diff --git a/gstreamer/src/element.rs b/gstreamer/src/element.rs index a975fc9f2..7f2e6d20c 100644 --- a/gstreamer/src/element.rs +++ b/gstreamer/src/element.rs @@ -20,7 +20,6 @@ use glib::translate::FromGlibPtrBorrow; use glib::translate::{ from_glib, from_glib_full, from_glib_none, FromGlib, FromGlibPtrContainer, ToGlib, ToGlibPtr, }; -use miniobject::MiniObject; use ClockTime; use ElementFlags; use Event; diff --git a/gstreamer/src/event.rs b/gstreamer/src/event.rs index 08032ae4f..949a65c79 100644 --- a/gstreamer/src/event.rs +++ b/gstreamer/src/event.rs @@ -7,7 +7,6 @@ // except according to those terms. use gst_sys; -use miniobject::*; use structure::*; use GenericFormattedValue; @@ -137,7 +136,7 @@ impl PartialOrd for EventType { } } -gst_define_mini_object_wrapper!(Event, EventRef, gst_sys::GstEvent, [Debug,], || { +gst_define_mini_object_wrapper!(Event, EventRef, gst_sys::GstEvent, || { gst_sys::gst_event_get_type() }); @@ -543,6 +542,12 @@ impl Event { } } +impl fmt::Debug for Event { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + EventRef::fmt(self, f) + } +} + impl fmt::Debug for EventRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Event") diff --git a/gstreamer/src/lib.rs b/gstreamer/src/lib.rs index cbeec163d..0aca5b86a 100644 --- a/gstreamer/src/lib.rs +++ b/gstreamer/src/lib.rs @@ -83,7 +83,6 @@ pub use error::*; #[macro_use] pub mod miniobject; -pub use miniobject::{GstRc, MiniObject}; pub mod message; pub use message::{Message, MessageErrorDomain, MessageRef, MessageView}; @@ -372,7 +371,6 @@ pub mod prelude { pub use typefind::TypeFindImpl; pub use value::GstValueExt; - pub use miniobject::MiniObject; pub use tags::{CustomTag, Tag}; pub use muldiv::MulDiv; diff --git a/gstreamer/src/memory.rs b/gstreamer/src/memory.rs index 3bec12c4b..4cfe309dc 100644 --- a/gstreamer/src/memory.rs +++ b/gstreamer/src/memory.rs @@ -18,13 +18,11 @@ use gst_sys; use glib; use glib::translate::{from_glib, from_glib_full, from_glib_none, ToGlibPtr}; -use miniobject::MiniObject; - use AllocationParams; use Allocator; use MemoryFlags; -gst_define_mini_object_wrapper!(Memory, MemoryRef, gst_sys::GstMemory, [Debug,], || { +gst_define_mini_object_wrapper!(Memory, MemoryRef, gst_sys::GstMemory, || { gst_sys::gst_memory_get_type() }); @@ -40,6 +38,12 @@ pub struct MappedMemory { phantom: PhantomData, } +impl fmt::Debug for Memory { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + MemoryRef::fmt(self, f) + } +} + impl fmt::Debug for MemoryRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Memory") diff --git a/gstreamer/src/message.rs b/gstreamer/src/message.rs index 814ea4d9a..0c30e1f76 100644 --- a/gstreamer/src/message.rs +++ b/gstreamer/src/message.rs @@ -7,7 +7,6 @@ // except according to those terms. use gst_sys; -use miniobject::*; use structure::*; use GenericFormattedValue; use GroupId; @@ -30,7 +29,7 @@ use glib::value::ToSendValue; use glib::Cast; use glib::IsA; -gst_define_mini_object_wrapper!(Message, MessageRef, gst_sys::GstMessage, [Debug,], || { +gst_define_mini_object_wrapper!(Message, MessageRef, gst_sys::GstMessage, || { gst_sys::gst_message_get_type() }); @@ -514,6 +513,12 @@ impl Message { } } +impl fmt::Debug for Message { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + MessageRef::fmt(self, f) + } +} + impl fmt::Debug for MessageRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Message") diff --git a/gstreamer/src/meta.rs b/gstreamer/src/meta.rs index 019090d92..d8d464e19 100644 --- a/gstreamer/src/meta.rs +++ b/gstreamer/src/meta.rs @@ -10,7 +10,6 @@ use std::fmt; use std::marker::PhantomData; use std::ops; -use miniobject::MiniObject; use Buffer; use BufferRef; #[cfg(any(feature = "v1_14", feature = "dox"))] diff --git a/gstreamer/src/miniobject.rs b/gstreamer/src/miniobject.rs index 61594bfda..a750e8bbc 100644 --- a/gstreamer/src/miniobject.rs +++ b/gstreamer/src/miniobject.rs @@ -1,4 +1,4 @@ -// Copyright (C) 2016-2017 Sebastian Dröge +// Copyright (C) 2016-2020 Sebastian Dröge // // Licensed under the Apache License, Version 2.0 or the MIT license @@ -6,573 +6,136 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::marker::PhantomData; -use std::mem; -use std::ptr; -use std::{borrow, fmt, ops}; - -use glib; -use glib::translate::*; -use glib_sys; -use glib_sys::gpointer; -use gobject_sys; -use gst_sys; - -pub struct GstRc { - obj: ptr::NonNull, - phantom: PhantomData, -} - -impl GstRc { - pub unsafe fn from_glib_none(ptr: *const T::GstType) -> Self { - skip_assert_initialized!(); - assert!(!ptr.is_null()); - - gst_sys::gst_mini_object_ref(ptr as *mut gst_sys::GstMiniObject); - - GstRc { - obj: ptr::NonNull::new_unchecked(ptr as *mut T::GstType as *mut T), - phantom: PhantomData, - } - } - - pub unsafe fn from_glib_full(ptr: *const T::GstType) -> Self { - assert!(!ptr.is_null()); - - GstRc { - obj: ptr::NonNull::new_unchecked(ptr as *mut T::GstType as *mut T), - phantom: PhantomData, - } - } - - pub unsafe fn from_glib_borrow(ptr: *const T::GstType) -> Borrowed { - assert!(!ptr.is_null()); - - Borrowed::new(GstRc { - obj: ptr::NonNull::new_unchecked(ptr as *mut T::GstType as *mut T), - phantom: PhantomData, - }) - } - - pub unsafe fn replace_ptr(&mut self, ptr: *mut T::GstType) { - assert!(!ptr.is_null()); - self.obj = ptr::NonNull::new_unchecked(ptr as *mut T); - } - - pub fn make_mut(&mut self) -> &mut T { - unsafe { - if self.is_writable() { - return self.obj.as_mut(); - } - - let ptr = gst_sys::gst_mini_object_make_writable( - self.as_mut_ptr() as *mut gst_sys::GstMiniObject - ); - self.replace_ptr(ptr as *mut T::GstType); - assert!(self.is_writable()); - - self.obj.as_mut() - } - } - - pub fn get_mut(&mut self) -> Option<&mut T> { - if self.is_writable() { - Some(unsafe { self.obj.as_mut() }) - } else { - None - } - } - - pub fn is_writable(&self) -> bool { - unsafe { - from_glib(gst_sys::gst_mini_object_is_writable( - self.as_ptr() as *const gst_sys::GstMiniObject - )) - } - } - - pub unsafe fn into_ptr(self) -> *mut T::GstType { - let s = mem::ManuallyDrop::new(self); - s.as_mut_ptr() - } - - #[cfg(any(feature = "v1_16", feature = "dox"))] - pub fn add_parent(&self, parent: &U) { - unsafe { - gst_sys::gst_mini_object_add_parent( - self.as_ptr() as *mut gst_sys::GstMiniObject, - parent.as_ptr() as *mut gst_sys::GstMiniObject, - ); - } - } - - #[cfg(any(feature = "v1_16", feature = "dox"))] - pub fn remove_parent(&self, parent: &U) { - unsafe { - gst_sys::gst_mini_object_remove_parent( - self.as_ptr() as *mut gst_sys::GstMiniObject, - parent.as_ptr() as *mut gst_sys::GstMiniObject, - ); - } - } -} - -impl ops::Deref for GstRc { - type Target = T; - fn deref(&self) -> &T { - self.as_ref() - } -} - -impl AsRef for GstRc { - fn as_ref(&self) -> &T { - unsafe { self.obj.as_ref() } - } -} - -impl borrow::Borrow for GstRc { - fn borrow(&self) -> &T { - self.as_ref() - } -} - -impl Clone for GstRc { - fn clone(&self) -> GstRc { - unsafe { GstRc::from_glib_none(self.as_ptr()) } - } -} - -impl Drop for GstRc { - fn drop(&mut self) { - unsafe { - gst_sys::gst_mini_object_unref(self.as_mut_ptr() as *mut gst_sys::GstMiniObject); - } - } -} - -unsafe impl Sync for GstRc {} -unsafe impl Send for GstRc {} - -impl PartialEq for GstRc { - fn eq(&self, other: &Self) -> bool { - self.as_ref().eq(other.as_ref()) - } -} - -impl Eq for GstRc {} - -impl fmt::Debug for GstRc { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.as_ref().fmt(f) - } -} - -impl fmt::Display for GstRc { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.as_ref().fmt(f) - } -} - -pub unsafe trait MiniObject -where - Self: Sized, -{ - type GstType; - - unsafe fn as_ptr(&self) -> *const Self::GstType { - self as *const Self as *const Self::GstType - } - - unsafe fn as_mut_ptr(&self) -> *mut Self::GstType { - self as *const Self as *mut Self::GstType - } - - unsafe fn from_ptr<'a>(ptr: *const Self::GstType) -> &'a Self { - assert!(!ptr.is_null()); - &*(ptr as *const Self) - } - - unsafe fn from_mut_ptr<'a>(ptr: *mut Self::GstType) -> &'a mut Self { - assert!(!ptr.is_null()); - assert_ne!( - gst_sys::gst_mini_object_is_writable(ptr as *mut gst_sys::GstMiniObject), - glib_sys::GFALSE - ); - &mut *(ptr as *mut Self) - } - - fn copy(&self) -> GstRc { - unsafe { - GstRc::from_glib_full(gst_sys::gst_mini_object_copy( - self.as_ptr() as *const gst_sys::GstMiniObject - ) as *const Self::GstType) - } - } -} - -impl<'a, T: MiniObject + 'static> ToGlibPtr<'a, *const T::GstType> for GstRc { - type Storage = &'a Self; - - fn to_glib_none(&'a self) -> Stash<'a, *const T::GstType, Self> { - Stash(unsafe { self.as_ptr() }, self) - } - - fn to_glib_full(&self) -> *const T::GstType { - unsafe { - gst_sys::gst_mini_object_ref(self.as_mut_ptr() as *mut gst_sys::GstMiniObject); - self.as_ptr() - } - } -} - -impl<'a, T: MiniObject + 'static> ToGlibPtr<'a, *mut T::GstType> for GstRc { - type Storage = &'a Self; - - fn to_glib_none(&'a self) -> Stash<'a, *mut T::GstType, Self> { - Stash(unsafe { self.as_mut_ptr() }, self) - } - - fn to_glib_full(&self) -> *mut T::GstType { - unsafe { - gst_sys::gst_mini_object_ref(self.as_mut_ptr() as *mut gst_sys::GstMiniObject); - self.as_mut_ptr() - } - } -} - -impl<'a, T: MiniObject + 'static> ToGlibPtrMut<'a, *mut T::GstType> for GstRc { - type Storage = &'a mut Self; - - fn to_glib_none_mut(&'a mut self) -> StashMut<*mut T::GstType, Self> { - self.make_mut(); - StashMut(unsafe { self.as_mut_ptr() }, self) - } -} - -impl<'a, T: MiniObject + 'static> ToGlibContainerFromSlice<'a, *mut *mut T::GstType> for GstRc { - #[allow(clippy::type_complexity)] - type Storage = ( - Vec>>, - Option>, - ); - - fn to_glib_none_from_slice(t: &'a [GstRc]) -> (*mut *mut T::GstType, Self::Storage) { - skip_assert_initialized!(); - let v: Vec<_> = t.iter().map(|s| s.to_glib_none()).collect(); - let mut v_ptr: Vec<_> = v.iter().map(|s| s.0).collect(); - v_ptr.push(ptr::null_mut() as *mut T::GstType); - - (v_ptr.as_ptr() as *mut *mut T::GstType, (v, Some(v_ptr))) - } - - fn to_glib_container_from_slice(t: &'a [GstRc]) -> (*mut *mut T::GstType, Self::Storage) { - skip_assert_initialized!(); - let v: Vec<_> = t.iter().map(|s| s.to_glib_none()).collect(); - - let v_ptr = unsafe { - let v_ptr = glib_sys::g_malloc0(mem::size_of::<*mut T::GstType>() * t.len() + 1) - as *mut *mut T::GstType; - - for (i, s) in v.iter().enumerate() { - ptr::write(v_ptr.add(i), s.0); - } - - v_ptr - }; - - (v_ptr, (v, None)) - } - - fn to_glib_full_from_slice(t: &[GstRc]) -> *mut *mut T::GstType { - skip_assert_initialized!(); - unsafe { - let v_ptr = glib_sys::g_malloc0(mem::size_of::<*mut T::GstType>() * t.len() + 1) - as *mut *mut T::GstType; - - for (i, s) in t.iter().enumerate() { - ptr::write(v_ptr.add(i), s.to_glib_full()); - } - - v_ptr - } - } -} - -impl<'a, T: MiniObject + 'static> ToGlibContainerFromSlice<'a, *const *mut T::GstType> - for GstRc -{ - #[allow(clippy::type_complexity)] - type Storage = ( - Vec>>, - Option>, - ); - - fn to_glib_none_from_slice(t: &'a [GstRc]) -> (*const *mut T::GstType, Self::Storage) { - skip_assert_initialized!(); - let (ptr, stash) = - ToGlibContainerFromSlice::<'a, *mut *mut T::GstType>::to_glib_none_from_slice(t); - (ptr as *const *mut T::GstType, stash) - } - - fn to_glib_container_from_slice(_: &'a [GstRc]) -> (*const *mut T::GstType, Self::Storage) { - skip_assert_initialized!(); - // Can't have consumer free a *const pointer - unimplemented!() - } - - fn to_glib_full_from_slice(_: &[GstRc]) -> *const *mut T::GstType { - skip_assert_initialized!(); - // Can't have consumer free a *const pointer - unimplemented!() - } -} - -impl FromGlibPtrNone<*const T::GstType> for GstRc { - unsafe fn from_glib_none(ptr: *const T::GstType) -> Self { - Self::from_glib_none(ptr) - } -} - -impl FromGlibPtrNone<*mut T::GstType> for GstRc { - unsafe fn from_glib_none(ptr: *mut T::GstType) -> Self { - Self::from_glib_none(ptr) - } -} - -impl FromGlibPtrFull<*const T::GstType> for GstRc { - unsafe fn from_glib_full(ptr: *const T::GstType) -> Self { - Self::from_glib_full(ptr) - } -} - -impl FromGlibPtrFull<*mut T::GstType> for GstRc { - unsafe fn from_glib_full(ptr: *mut T::GstType) -> Self { - Self::from_glib_full(ptr) - } -} - -impl FromGlibPtrBorrow<*const T::GstType> for GstRc { - unsafe fn from_glib_borrow(ptr: *const T::GstType) -> Borrowed { - Self::from_glib_borrow(ptr) - } -} - -impl FromGlibPtrBorrow<*mut T::GstType> for GstRc { - unsafe fn from_glib_borrow(ptr: *mut T::GstType) -> Borrowed { - Self::from_glib_borrow(ptr) - } -} - -impl FromGlibContainerAsVec<*mut T::GstType, *mut *mut T::GstType> - for GstRc -{ - unsafe fn from_glib_none_num_as_vec(ptr: *mut *mut T::GstType, num: usize) -> Vec { - if num == 0 || ptr.is_null() { - return Vec::new(); - } - - let mut res = Vec::with_capacity(num); - for i in 0..num { - res.push(from_glib_none(ptr::read(ptr.add(i)))); - } - res - } - - unsafe fn from_glib_container_num_as_vec(ptr: *mut *mut T::GstType, num: usize) -> Vec { - let res = FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, num); - glib_sys::g_free(ptr as *mut _); - res - } - - unsafe fn from_glib_full_num_as_vec(ptr: *mut *mut T::GstType, num: usize) -> Vec { - if num == 0 || ptr.is_null() { - return Vec::new(); - } - - let mut res = Vec::with_capacity(num); - for i in 0..num { - res.push(from_glib_full(ptr::read(ptr.add(i)))); - } - glib_sys::g_free(ptr as *mut _); - res - } -} - -impl FromGlibPtrArrayContainerAsVec<*mut T::GstType, *mut *mut T::GstType> - for GstRc -{ - unsafe fn from_glib_none_as_vec(ptr: *mut *mut T::GstType) -> Vec { - FromGlibContainerAsVec::from_glib_none_num_as_vec( - ptr, - glib::translate::c_ptr_array_len(ptr), - ) - } - - unsafe fn from_glib_container_as_vec(ptr: *mut *mut T::GstType) -> Vec { - FromGlibContainerAsVec::from_glib_container_num_as_vec( - ptr, - glib::translate::c_ptr_array_len(ptr), - ) - } - - unsafe fn from_glib_full_as_vec(ptr: *mut *mut T::GstType) -> Vec { - FromGlibContainerAsVec::from_glib_full_num_as_vec( - ptr, - glib::translate::c_ptr_array_len(ptr), - ) - } -} - -impl FromGlibContainerAsVec<*mut T::GstType, *const *mut T::GstType> - for GstRc -{ - unsafe fn from_glib_none_num_as_vec(ptr: *const *mut T::GstType, num: usize) -> Vec { - FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr as *mut *mut _, num) - } - - unsafe fn from_glib_container_num_as_vec(_: *const *mut T::GstType, _: usize) -> Vec { - // Can't free a *const - unimplemented!() - } - - unsafe fn from_glib_full_num_as_vec(_: *const *mut T::GstType, _: usize) -> Vec { - // Can't free a *const - unimplemented!() - } -} - -impl - FromGlibPtrArrayContainerAsVec<*mut T::GstType, *const *mut T::GstType> for GstRc -{ - unsafe fn from_glib_none_as_vec(ptr: *const *mut T::GstType) -> Vec { - FromGlibPtrArrayContainerAsVec::from_glib_none_as_vec(ptr as *mut *mut _) - } - - unsafe fn from_glib_container_as_vec(_: *const *mut T::GstType) -> Vec { - // Can't free a *const - unimplemented!() - } - - unsafe fn from_glib_full_as_vec(_: *const *mut T::GstType) -> Vec { - // Can't free a *const - unimplemented!() - } -} - -impl glib::StaticType for GstRc { - fn static_type() -> glib::types::Type { - T::static_type() - } -} - -impl<'a, T: MiniObject + glib::StaticType + 'static> glib::value::FromValueOptional<'a> - for GstRc -{ - unsafe fn from_value_optional(v: &'a glib::Value) -> Option { - let ptr = gobject_sys::g_value_get_boxed(v.to_glib_none().0); - from_glib_none(ptr as *const T::GstType) - } -} - -impl glib::value::SetValue for GstRc { - unsafe fn set_value(v: &mut glib::Value, s: &Self) { - gobject_sys::g_value_set_boxed(v.to_glib_none_mut().0, s.as_ptr() as gpointer); - } -} - -impl glib::value::SetValueOptional for GstRc { - unsafe fn set_value_optional(v: &mut glib::Value, s: Option<&Self>) { - if let Some(s) = s { - gobject_sys::g_value_set_boxed(v.to_glib_none_mut().0, s.as_ptr() as gpointer); - } else { - gobject_sys::g_value_set_boxed(v.to_glib_none_mut().0, ptr::null_mut()); - } - } -} - -impl GlibPtrDefault for GstRc { - type GlibType = *mut T::GstType; -} - #[macro_export] macro_rules! gst_define_mini_object_wrapper( - ($name:ident, $ref_name:ident, $gst_sys_name:path, [$($derives:ident,)*], $get_type:expr) => { - #[derive($($derives,)*)] - #[derive(Clone)] - pub struct $name($crate::GstRc<$ref_name>); + ($name:ident, $ref_name:ident, $gst_sys_name:path, $get_type:expr) => { + pub struct $name { + obj: ::std::ptr::NonNull<$ref_name>, + phantom: ::std::marker::PhantomData<$ref_name>, + } #[repr(C)] pub struct $ref_name($gst_sys_name); impl $name { pub unsafe fn from_glib_none(ptr: *const $gst_sys_name) -> Self { - $name($crate::glib::translate::from_glib_none(ptr)) + skip_assert_initialized!(); + assert!(!ptr.is_null()); + + $crate::gst_sys::gst_mini_object_ref(ptr as *mut $crate::gst_sys::GstMiniObject); + + $name { + obj: ::std::ptr::NonNull::new_unchecked(ptr as *mut $gst_sys_name as *mut $ref_name), + phantom: ::std::marker::PhantomData, + } } pub unsafe fn from_glib_full(ptr: *const $gst_sys_name) -> Self { - $name($crate::glib::translate::from_glib_full(ptr)) + skip_assert_initialized!(); + assert!(!ptr.is_null()); + + $name { + obj: ::std::ptr::NonNull::new_unchecked(ptr as *mut $gst_sys_name as *mut $ref_name), + phantom: ::std::marker::PhantomData, + } } pub unsafe fn from_glib_borrow(ptr: *const $gst_sys_name) -> $crate::glib::translate::Borrowed { - $crate::glib::translate::Borrowed::new( - $name( - $crate::glib::translate::from_glib_borrow::<_, $crate::GstRc::<$ref_name>>(ptr) - .into_inner() - ) - ) + skip_assert_initialized!(); + assert!(!ptr.is_null()); + + $crate::glib::translate::Borrowed::new($name { + obj: ::std::ptr::NonNull::new_unchecked(ptr as *mut $gst_sys_name as *mut $ref_name), + phantom: ::std::marker::PhantomData, + }) + } + + pub unsafe fn replace_ptr(&mut self, ptr: *mut $gst_sys_name) { + assert!(!ptr.is_null()); + self.obj = ::std::ptr::NonNull::new_unchecked(ptr as *mut $ref_name); + } + + pub fn make_mut(&mut self) -> &mut $ref_name { + unsafe { + if self.is_writable() { + return self.obj.as_mut(); + } + + let ptr = $crate::gst_sys::gst_mini_object_make_writable( + self.as_mut_ptr() as *mut $crate::gst_sys::GstMiniObject + ); + self.replace_ptr(ptr as *mut $gst_sys_name); + assert!(self.is_writable()); + + self.obj.as_mut() + } + } + + pub fn get_mut(&mut self) -> Option<&mut $ref_name> { + if self.is_writable() { + Some(unsafe { self.obj.as_mut() }) + } else { + None + } + } + + pub fn is_writable(&self) -> bool { + unsafe { + $crate::glib::translate::from_glib($crate::gst_sys::gst_mini_object_is_writable( + self.as_ptr() as *const $crate::gst_sys::GstMiniObject + )) + } } pub unsafe fn into_ptr(self) -> *mut $gst_sys_name { - self.0.into_ptr() - } - - pub fn copy(&self) -> Self { - self.0.copy() + let s = ::std::mem::ManuallyDrop::new(self); + s.as_mut_ptr() } } - impl $ref_name { - fn copy(&self) -> $name { - $name(<$ref_name as $crate::MiniObject>::copy(self)) + impl Clone for $name { + fn clone(&self) -> Self { + unsafe { $name::from_glib_none(self.as_ptr()) } } } - impl From<$crate::GstRc<$ref_name>> for $name { - fn from(rc: $crate::GstRc<$ref_name>) -> $name { - skip_assert_initialized!(); - $name(rc) - } - } - - impl Into<$crate::GstRc<$ref_name>> for $name { - fn into(self) -> $crate::GstRc<$ref_name> { - self.0 + impl Drop for $name { + fn drop(&mut self) { + unsafe { + $crate::gst_sys::gst_mini_object_unref(self.as_mut_ptr() as *mut $crate::gst_sys::GstMiniObject); + } } } impl ::std::ops::Deref for $name { - type Target = $crate::GstRc<$ref_name>; + type Target = $ref_name; fn deref(&self) -> &Self::Target { - &self.0 + unsafe { &*(self.obj.as_ptr() as *const Self::Target) } } } impl ::std::ops::DerefMut for $name { fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 + unsafe { &mut *(self.obj.as_ptr() as *mut Self::Target) } } } impl AsRef<$ref_name> for $name { fn as_ref(&self) -> &$ref_name { - self.0.as_ref() + &*self + } + } + + impl AsMut<$ref_name> for $name { + fn as_mut(&mut self) -> &mut $ref_name { + &mut *self } } impl ::std::borrow::Borrow<$ref_name> for $name { fn borrow(&self) -> &$ref_name { - self.as_ref() + &*self } } @@ -583,44 +146,48 @@ macro_rules! gst_define_mini_object_wrapper( } impl<'a> $crate::glib::translate::ToGlibPtr<'a, *const $gst_sys_name> for $name { - type Storage = $crate::glib::translate::Stash<'a, *const $gst_sys_name, $crate::GstRc<$ref_name>>; + type Storage = &'a Self; fn to_glib_none(&'a self) -> $crate::glib::translate::Stash<'a, *const $gst_sys_name, Self> { - let stash = $crate::glib::translate::ToGlibPtr::to_glib_none(&self.0); - $crate::glib::translate::Stash(stash.0, stash) + $crate::glib::translate::Stash(unsafe { self.as_ptr() }, self) } fn to_glib_full(&self) -> *const $gst_sys_name { - $crate::glib::translate::ToGlibPtr::to_glib_full(&self.0) + unsafe { + $crate::gst_sys::gst_mini_object_ref(self.as_mut_ptr() as *mut $crate::gst_sys::GstMiniObject); + self.as_ptr() + } } } impl<'a> $crate::glib::translate::ToGlibPtr<'a, *mut $gst_sys_name> for $name { - type Storage = $crate::glib::translate::Stash<'a, *mut $gst_sys_name, $crate::GstRc<$ref_name>>; + type Storage = &'a Self; fn to_glib_none(&'a self) -> $crate::glib::translate::Stash<'a, *mut $gst_sys_name, Self> { - let stash = $crate::glib::translate::ToGlibPtr::to_glib_none(&self.0); - $crate::glib::translate::Stash(stash.0, stash) + $crate::glib::translate::Stash(unsafe { self.as_mut_ptr() }, self) } fn to_glib_full(&self) -> *mut $gst_sys_name { - $crate::glib::translate::ToGlibPtr::to_glib_full(&self.0) + unsafe { + $crate::gst_sys::gst_mini_object_ref(self.as_mut_ptr() as *mut $crate::gst_sys::GstMiniObject); + self.as_mut_ptr() + } } } impl<'a> $crate::glib::translate::ToGlibPtrMut<'a, *mut $gst_sys_name> for $name { - type Storage = $crate::glib::translate::StashMut<'a, *mut $gst_sys_name, $crate::GstRc<$ref_name>>; + type Storage = &'a mut Self; fn to_glib_none_mut(&'a mut self) -> $crate::glib::translate::StashMut<*mut $gst_sys_name, Self> { - let stash = $crate::glib::translate::ToGlibPtrMut::to_glib_none_mut(&mut self.0); - $crate::glib::translate::StashMut(stash.0, stash) + self.make_mut(); + $crate::glib::translate::StashMut(unsafe { self.as_mut_ptr() }, self) } } impl<'a> $crate::glib::translate::ToGlibContainerFromSlice<'a, *mut *mut $gst_sys_name> for $name { #[allow(clippy::type_complexity)] type Storage = ( - Vec<$crate::glib::translate::Stash<'a, *mut $gst_sys_name, $name>>, + Vec<$crate::glib::translate::Stash<'a, *mut $gst_sys_name, Self>>, Option>, ); @@ -658,7 +225,7 @@ macro_rules! gst_define_mini_object_wrapper( as *mut *mut $gst_sys_name; for (i, s) in t.iter().enumerate() { - ::std::ptr::write(v_ptr.add(i), $crate::glib::translate::ToGlibPtr::to_glib_full(&s)); + ::std::ptr::write(v_ptr.add(i), $crate::glib::translate::ToGlibPtr::to_glib_full(s)); } v_ptr @@ -821,19 +388,24 @@ macro_rules! gst_define_mini_object_wrapper( for $name { unsafe fn from_value_optional(v: &'a glib::Value) -> Option { - <$crate::GstRc<$ref_name> as $crate::glib::value::FromValueOptional>::from_value_optional(v).map($name) + let ptr = $crate::gobject_sys::g_value_get_boxed($crate::glib::translate::ToGlibPtr::to_glib_none(v).0); + $crate::glib::translate::from_glib_none(ptr as *const $gst_sys_name) } } impl $crate::glib::value::SetValue for $name { unsafe fn set_value(v: &mut glib::Value, s: &Self) { - <$crate::GstRc<$ref_name> as $crate::glib::value::SetValue>::set_value(v, &s.0) + $crate::gobject_sys::g_value_set_boxed($crate::glib::translate::ToGlibPtrMut::to_glib_none_mut(v).0, s.as_ptr() as $crate::glib_sys::gpointer); } } impl $crate::glib::value::SetValueOptional for $name { unsafe fn set_value_optional(v: &mut glib::Value, s: Option<&Self>) { - <$crate::GstRc<$ref_name> as $crate::glib::value::SetValueOptional>::set_value_optional(v, s.map(|s| &s.0)) + if let Some(s) = s { + $crate::gobject_sys::g_value_set_boxed($crate::glib::translate::ToGlibPtrMut::to_glib_none_mut(v).0, s.as_ptr() as $crate::glib_sys::gpointer); + } else { + $crate::gobject_sys::g_value_set_boxed($crate::glib::translate::ToGlibPtrMut::to_glib_none_mut(v).0, ::std::ptr::null_mut()); + } } } @@ -841,8 +413,36 @@ macro_rules! gst_define_mini_object_wrapper( type GlibType = *mut $gst_sys_name; } - unsafe impl $crate::MiniObject for $ref_name { - type GstType = $gst_sys_name; + impl $ref_name { + pub unsafe fn as_ptr(&self) -> *const $gst_sys_name { + self as *const Self as *const $gst_sys_name + } + + pub unsafe fn as_mut_ptr(&self) -> *mut $gst_sys_name { + self as *const Self as *mut $gst_sys_name + } + + pub unsafe fn from_ptr<'a>(ptr: *const $gst_sys_name) -> &'a Self { + assert!(!ptr.is_null()); + &*(ptr as *const Self) + } + + pub unsafe fn from_mut_ptr<'a>(ptr: *mut $gst_sys_name) -> &'a mut Self { + assert!(!ptr.is_null()); + assert_ne!( + $crate::gst_sys::gst_mini_object_is_writable(ptr as *mut $crate::gst_sys::GstMiniObject), + $crate::glib_sys::GFALSE + ); + &mut *(ptr as *mut Self) + } + + pub fn copy(&self) -> $name { + unsafe { + $name::from_glib_full($crate::gst_sys::gst_mini_object_copy( + self.as_ptr() as *const $crate::gst_sys::GstMiniObject + ) as *const $gst_sys_name) + } + } } impl $crate::glib::types::StaticType for $ref_name { @@ -851,6 +451,10 @@ macro_rules! gst_define_mini_object_wrapper( } } + impl $crate::glib::translate::GlibPtrDefault for $ref_name { + type GlibType = *mut $gst_sys_name; + } + impl<'a> $crate::glib::value::FromValueOptional<'a> for &'a $ref_name { @@ -877,5 +481,7 @@ macro_rules! gst_define_mini_object_wrapper( unsafe impl Sync for $ref_name {} unsafe impl Send for $ref_name {} + unsafe impl Sync for $name {} + unsafe impl Send for $name {} } ); diff --git a/gstreamer/src/pad.rs b/gstreamer/src/pad.rs index 97af74829..ef158a7d0 100644 --- a/gstreamer/src/pad.rs +++ b/gstreamer/src/pad.rs @@ -6,7 +6,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use miniobject::MiniObject; use Buffer; use BufferList; use Event; diff --git a/gstreamer/src/query.rs b/gstreamer/src/query.rs index 27109f4af..56f6bfcb5 100644 --- a/gstreamer/src/query.rs +++ b/gstreamer/src/query.rs @@ -7,7 +7,6 @@ // except according to those terms. use gst_sys; -use miniobject::*; use structure::*; use GenericFormattedValue; @@ -21,7 +20,7 @@ use glib; use glib::translate::*; use glib_sys; -gst_define_mini_object_wrapper!(Query, QueryRef, gst_sys::GstQuery, [Debug,], || { +gst_define_mini_object_wrapper!(Query, QueryRef, gst_sys::GstQuery, || { gst_sys::gst_query_get_type() }); @@ -243,6 +242,12 @@ impl QueryRef { } } +impl fmt::Debug for Query { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + QueryRef::fmt(self, f) + } +} + impl fmt::Debug for QueryRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Query") @@ -266,31 +271,31 @@ pub unsafe trait AsMutPtr: AsPtr { unsafe impl AsPtr for Query { unsafe fn as_ptr(&self) -> *mut gst_sys::GstQuery { - self.as_ref().as_ptr() as *mut gst_sys::GstQuery + QueryRef::as_ptr(self) as *mut gst_sys::GstQuery } } unsafe impl AsMutPtr for Query { unsafe fn as_mut_ptr(&self) -> *mut gst_sys::GstQuery { - self.as_ref().as_mut_ptr() + QueryRef::as_ptr(self) as *mut gst_sys::GstQuery } } unsafe impl<'a> AsPtr for &'a QueryRef { unsafe fn as_ptr(&self) -> *mut gst_sys::GstQuery { - MiniObject::as_ptr(self as &QueryRef) as *mut gst_sys::GstQuery + QueryRef::as_ptr(self) as *mut gst_sys::GstQuery } } unsafe impl<'a> AsPtr for &'a mut QueryRef { unsafe fn as_ptr(&self) -> *mut gst_sys::GstQuery { - MiniObject::as_ptr(self as &QueryRef) as *mut gst_sys::GstQuery + QueryRef::as_ptr(self) as *mut gst_sys::GstQuery } } unsafe impl<'a> AsMutPtr for &'a mut QueryRef { unsafe fn as_mut_ptr(&self) -> *mut gst_sys::GstQuery { - MiniObject::as_mut_ptr(self as &QueryRef) as *mut gst_sys::GstQuery + QueryRef::as_ptr(self) as *mut gst_sys::GstQuery } } diff --git a/gstreamer/src/sample.rs b/gstreamer/src/sample.rs index b85d6bc46..c69d2ecae 100644 --- a/gstreamer/src/sample.rs +++ b/gstreamer/src/sample.rs @@ -14,7 +14,6 @@ use gst_sys; use glib; use glib::translate::{from_glib_full, from_glib_none, mut_override, ToGlibPtr}; -use miniobject::*; use Buffer; use BufferList; use BufferListRef; @@ -27,7 +26,7 @@ use Segment; use Structure; use StructureRef; -gst_define_mini_object_wrapper!(Sample, SampleRef, gst_sys::GstSample, [Debug,], || { +gst_define_mini_object_wrapper!(Sample, SampleRef, gst_sys::GstSample, || { gst_sys::gst_sample_get_type() }); @@ -215,6 +214,12 @@ impl SampleRef { } } +impl fmt::Debug for Sample { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + SampleRef::fmt(self, f) + } +} + impl fmt::Debug for SampleRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Sample") diff --git a/gstreamer/src/tags.rs b/gstreamer/src/tags.rs index 432dafaf9..8b546f50b 100644 --- a/gstreamer/src/tags.rs +++ b/gstreamer/src/tags.rs @@ -22,8 +22,6 @@ use glib::StaticType; use gobject_sys; use gst_sys; -use miniobject::*; - use Sample; use TagError; use TagMergeMode; @@ -341,13 +339,9 @@ impl_tag!( ); impl_tag!(PrivateData, Sample, TAG_PRIVATE_DATA, GST_TAG_PRIVATE_DATA); -gst_define_mini_object_wrapper!( - TagList, - TagListRef, - gst_sys::GstTagList, - [Debug, PartialEq, Eq,], - || gst_sys::gst_tag_list_get_type() -); +gst_define_mini_object_wrapper!(TagList, TagListRef, gst_sys::GstTagList, || { + gst_sys::gst_tag_list_get_type() +}); impl TagList { pub fn new() -> Self { @@ -362,12 +356,6 @@ impl Default for TagList { } } -impl fmt::Display for TagList { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.0.fmt(f) - } -} - impl TagListRef { pub fn add<'a, T: Tag<'a>>(&mut self, value: &T::TagType, mode: TagMergeMode) where @@ -513,6 +501,26 @@ impl TagListRef { } } +impl fmt::Debug for TagList { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + ::fmt(self, f) + } +} + +impl fmt::Display for TagList { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + ::fmt(self, f) + } +} + +impl PartialEq for TagList { + fn eq(&self, other: &TagList) -> bool { + TagListRef::eq(self, other) + } +} + +impl Eq for TagList {} + impl fmt::Debug for TagListRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_tuple("TagList").field(&self.to_string()).finish() diff --git a/gstreamer/src/toc.rs b/gstreamer/src/toc.rs index 9910a7753..0a81b3c16 100644 --- a/gstreamer/src/toc.rs +++ b/gstreamer/src/toc.rs @@ -17,14 +17,13 @@ use glib::translate::{ from_glib, from_glib_full, from_glib_none, FromGlibPtrContainer, ToGlib, ToGlibPtr, }; -use miniobject::*; use TagList; use TagMergeMode; use TocEntryType; use TocLoopType; use TocScope; -gst_define_mini_object_wrapper!(Toc, TocRef, gst_sys::GstToc, [Debug,], || { +gst_define_mini_object_wrapper!(Toc, TocRef, gst_sys::GstToc, || { gst_sys::gst_toc_get_type() }); @@ -82,6 +81,12 @@ impl TocRef { } } +impl fmt::Debug for Toc { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + TocRef::fmt(self, f) + } +} + impl fmt::Debug for TocRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Toc") @@ -92,13 +97,9 @@ impl fmt::Debug for TocRef { } } -gst_define_mini_object_wrapper!( - TocEntry, - TocEntryRef, - gst_sys::GstTocEntry, - [Debug,], - || gst_sys::gst_toc_entry_get_type() -); +gst_define_mini_object_wrapper!(TocEntry, TocEntryRef, gst_sys::GstTocEntry, || { + gst_sys::gst_toc_entry_get_type() +}); impl TocEntry { pub fn new(type_: TocEntryType, uid: &str) -> Self { @@ -220,6 +221,12 @@ impl TocEntryRef { } } +impl fmt::Debug for TocEntry { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + TocEntryRef::fmt(self, f) + } +} + impl fmt::Debug for TocEntryRef { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("TocEntry")