Make use of the new TransparentType / TransparentPtrType traits

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1178>
This commit is contained in:
Sebastian Dröge 2023-01-02 19:03:08 +02:00
parent aeca82c095
commit 882513d33a
15 changed files with 47 additions and 5 deletions

View file

@ -42,6 +42,7 @@ impl IntoGlib for AudioEndianness {
} }
#[doc(alias = "GstAudioFormatInfo")] #[doc(alias = "GstAudioFormatInfo")]
#[derive(Copy, Clone)]
pub struct AudioFormatInfo(&'static ffi::GstAudioFormatInfo); pub struct AudioFormatInfo(&'static ffi::GstAudioFormatInfo);
impl AudioFormatInfo { impl AudioFormatInfo {
@ -379,6 +380,9 @@ impl glib::translate::GlibPtrDefault for AudioFormatInfo {
type GlibType = *mut ffi::GstAudioFormatInfo; type GlibType = *mut ffi::GstAudioFormatInfo;
} }
#[doc(hidden)]
unsafe impl glib::translate::TransparentPtrType for AudioFormatInfo {}
#[doc(hidden)] #[doc(hidden)]
impl<'a> glib::translate::ToGlibPtr<'a, *const ffi::GstAudioFormatInfo> for AudioFormatInfo { impl<'a> glib::translate::ToGlibPtr<'a, *const ffi::GstAudioFormatInfo> for AudioFormatInfo {
type Storage = PhantomData<&'a Self>; type Storage = PhantomData<&'a Self>;

View file

@ -512,6 +512,9 @@ impl glib::translate::GlibPtrDefault for VideoFormatInfo {
type GlibType = *mut ffi::GstVideoFormatInfo; type GlibType = *mut ffi::GstVideoFormatInfo;
} }
#[doc(hidden)]
unsafe impl glib::translate::TransparentPtrType for VideoFormatInfo {}
#[doc(hidden)] #[doc(hidden)]
impl<'a> glib::translate::ToGlibPtr<'a, *const ffi::GstVideoFormatInfo> for VideoFormatInfo { impl<'a> glib::translate::ToGlibPtr<'a, *const ffi::GstVideoFormatInfo> for VideoFormatInfo {
type Storage = PhantomData<&'a Self>; type Storage = PhantomData<&'a Self>;

View file

@ -351,6 +351,11 @@ macro_rules! generic_impl {
type GlibType = *mut ffi::GstVideoTimeCode; type GlibType = *mut ffi::GstVideoTimeCode;
} }
#[doc(hidden)]
unsafe impl TransparentType for $name {
type GlibType = ffi::GstVideoTimeCode;
}
#[doc(hidden)] #[doc(hidden)]
impl<'a> ToGlibPtr<'a, *const ffi::GstVideoTimeCode> for $name { impl<'a> ToGlibPtr<'a, *const ffi::GstVideoTimeCode> for $name {
type Storage = PhantomData<&'a Self>; type Storage = PhantomData<&'a Self>;

View file

@ -136,6 +136,11 @@ impl GlibPtrDefault for VideoTimeCodeInterval {
type GlibType = *mut ffi::GstVideoTimeCodeInterval; type GlibType = *mut ffi::GstVideoTimeCodeInterval;
} }
#[doc(hidden)]
unsafe impl TransparentType for VideoTimeCodeInterval {
type GlibType = ffi::GstVideoTimeCodeInterval;
}
#[doc(hidden)] #[doc(hidden)]
impl<'a> ToGlibPtr<'a, *const ffi::GstVideoTimeCodeInterval> for VideoTimeCodeInterval { impl<'a> ToGlibPtr<'a, *const ffi::GstVideoTimeCodeInterval> for VideoTimeCodeInterval {
type Storage = PhantomData<&'a Self>; type Storage = PhantomData<&'a Self>;

View file

@ -300,6 +300,8 @@ impl GlibPtrDefault for CapsFeatures {
type GlibType = *mut ffi::GstCapsFeatures; type GlibType = *mut ffi::GstCapsFeatures;
} }
unsafe impl TransparentPtrType for CapsFeatures {}
#[repr(transparent)] #[repr(transparent)]
#[doc(alias = "GstCapsFeatures")] #[doc(alias = "GstCapsFeatures")]
pub struct CapsFeaturesRef(ffi::GstCapsFeatures); pub struct CapsFeaturesRef(ffi::GstCapsFeatures);

View file

@ -996,7 +996,7 @@ pub unsafe trait ElementClassExt {
unsafe { unsafe {
let klass = self as *const _ as *const ffi::GstElementClass; let klass = self as *const _ as *const ffi::GstElementClass;
glib::List::from_glib_none_static(ffi::gst_element_class_get_pad_template_list( glib::List::from_glib_none(ffi::gst_element_class_get_pad_template_list(
klass as *mut _, klass as *mut _,
)) ))
} }

View file

@ -101,9 +101,9 @@ impl ElementFactory {
#[doc(alias = "get_static_pad_templates")] #[doc(alias = "get_static_pad_templates")]
pub fn static_pad_templates(&self) -> glib::List<StaticPadTemplate> { pub fn static_pad_templates(&self) -> glib::List<StaticPadTemplate> {
unsafe { unsafe {
glib::List::from_glib_none_static(ffi::gst_element_factory_get_static_pad_templates( glib::List::from_glib_none(ffi::gst_element_factory_get_static_pad_templates(
self.to_glib_none().0, self.to_glib_none().0,
) as *mut _) ))
} }
} }

View file

@ -568,6 +568,9 @@ impl<T> glib::translate::GlibPtrDefault for Iterator<T> {
type GlibType = *mut ffi::GstIterator; type GlibType = *mut ffi::GstIterator;
} }
#[doc(hidden)]
unsafe impl<T: StaticType + 'static> TransparentPtrType for Iterator<T> {}
#[doc(hidden)] #[doc(hidden)]
impl<'a, T: 'static> glib::translate::ToGlibPtr<'a, *const ffi::GstIterator> for Iterator<T> { impl<'a, T: 'static> glib::translate::ToGlibPtr<'a, *const ffi::GstIterator> for Iterator<T> {
type Storage = PhantomData<&'a Iterator<T>>; type Storage = PhantomData<&'a Iterator<T>>;

View file

@ -394,7 +394,7 @@ impl DebugCategory {
#[doc(alias = "get_all_categories")] #[doc(alias = "get_all_categories")]
#[doc(alias = "gst_debug_get_all_categories")] #[doc(alias = "gst_debug_get_all_categories")]
pub fn all_categories() -> glib::SList<DebugCategory> { pub fn all_categories() -> glib::SList<DebugCategory> {
unsafe { glib::SList::from_glib_container_static(ffi::gst_debug_get_all_categories()) } unsafe { glib::SList::from_glib_container(ffi::gst_debug_get_all_categories()) }
} }
#[cfg(any(feature = "v1_18", feature = "dox"))] #[cfg(any(feature = "v1_18", feature = "dox"))]
@ -438,6 +438,8 @@ impl GlibPtrDefault for DebugCategory {
type GlibType = *mut ffi::GstDebugCategory; type GlibType = *mut ffi::GstDebugCategory;
} }
unsafe impl TransparentPtrType for DebugCategory {}
impl FromGlibPtrNone<*mut ffi::GstDebugCategory> for DebugCategory { impl FromGlibPtrNone<*mut ffi::GstDebugCategory> for DebugCategory {
unsafe fn from_glib_none(ptr: *mut ffi::GstDebugCategory) -> Self { unsafe fn from_glib_none(ptr: *mut ffi::GstDebugCategory) -> Self {
assert!(!ptr.is_null()); assert!(!ptr.is_null());
@ -1216,7 +1218,9 @@ mod tests {
fn all() { fn all() {
crate::init().unwrap(); crate::init().unwrap();
assert!(DebugCategory::all_categories().any(|c| c.name() == "GST_PERFORMANCE")); assert!(DebugCategory::all_categories()
.iter()
.any(|c| c.name() == "GST_PERFORMANCE"));
} }
#[test] #[test]

View file

@ -387,6 +387,8 @@ macro_rules! mini_object_wrapper (
type GlibType = *mut $ffi_name; type GlibType = *mut $ffi_name;
} }
unsafe impl $crate::glib::translate::TransparentPtrType for $name {}
impl $ref_name { impl $ref_name {
pub fn as_ptr(&self) -> *const $ffi_name { pub fn as_ptr(&self) -> *const $ffi_name {
self as *const Self as *const $ffi_name self as *const Self as *const $ffi_name

View file

@ -656,6 +656,11 @@ impl<T: FormattedValueIntrinsic> glib::translate::GlibPtrDefault for FormattedSe
type GlibType = *mut ffi::GstSegment; type GlibType = *mut ffi::GstSegment;
} }
#[doc(hidden)]
unsafe impl<T: FormattedValueIntrinsic> TransparentType for FormattedSegment<T> {
type GlibType = ffi::GstSegment;
}
#[doc(hidden)] #[doc(hidden)]
impl<'a, T: FormattedValueIntrinsic> glib::translate::ToGlibPtr<'a, *const ffi::GstSegment> impl<'a, T: FormattedValueIntrinsic> glib::translate::ToGlibPtr<'a, *const ffi::GstSegment>
for FormattedSegment<T> for FormattedSegment<T>

View file

@ -11,6 +11,7 @@ use std::marker::PhantomData;
use std::ptr; use std::ptr;
#[doc(alias = "GstStaticCaps")] #[doc(alias = "GstStaticCaps")]
#[derive(Clone, Copy)]
pub struct StaticCaps(ptr::NonNull<ffi::GstStaticCaps>); pub struct StaticCaps(ptr::NonNull<ffi::GstStaticCaps>);
impl StaticCaps { impl StaticCaps {
@ -43,6 +44,8 @@ impl glib::value::ValueType for StaticCaps {
type Type = Self; type Type = Self;
} }
unsafe impl glib::translate::TransparentPtrType for StaticCaps {}
#[doc(hidden)] #[doc(hidden)]
unsafe impl<'a> glib::value::FromValue<'a> for StaticCaps { unsafe impl<'a> glib::value::FromValue<'a> for StaticCaps {
type Checker = glib::value::GenericValueTypeOrNoneChecker<Self>; type Checker = glib::value::GenericValueTypeOrNoneChecker<Self>;

View file

@ -12,6 +12,7 @@ use std::marker::PhantomData;
use std::ptr; use std::ptr;
#[doc(alias = "GstStaticPadTemplate")] #[doc(alias = "GstStaticPadTemplate")]
#[derive(Clone, Copy)]
pub struct StaticPadTemplate(ptr::NonNull<ffi::GstStaticPadTemplate>); pub struct StaticPadTemplate(ptr::NonNull<ffi::GstStaticPadTemplate>);
impl StaticPadTemplate { impl StaticPadTemplate {
@ -43,6 +44,8 @@ impl StaticPadTemplate {
} }
} }
unsafe impl glib::translate::TransparentPtrType for StaticPadTemplate {}
unsafe impl Send for StaticPadTemplate {} unsafe impl Send for StaticPadTemplate {}
unsafe impl Sync for StaticPadTemplate {} unsafe impl Sync for StaticPadTemplate {}

View file

@ -368,6 +368,8 @@ impl GlibPtrDefault for Structure {
type GlibType = *mut ffi::GstStructure; type GlibType = *mut ffi::GstStructure;
} }
unsafe impl TransparentPtrType for Structure {}
#[repr(transparent)] #[repr(transparent)]
#[doc(alias = "GstStructure")] #[doc(alias = "GstStructure")]
pub struct StructureRef(ffi::GstStructure); pub struct StructureRef(ffi::GstStructure);

View file

@ -254,6 +254,7 @@ mod tests {
crate::init().unwrap(); crate::init().unwrap();
let xml_factory = TypeFindFactory::factories() let xml_factory = TypeFindFactory::factories()
.into_iter()
.find(|f| { .find(|f| {
f.caps() f.caps()
.map(|c| { .map(|c| {