Skip to main content

gstreamer/
miniobject.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::fmt;
4
5use crate::{ffi, prelude::*};
6use glib::translate::*;
7
8pub trait IsMiniObject:
9    AsRef<Self::RefType> + FromGlibPtrFull<*mut Self::FfiType> + Send + Sync + 'static
10{
11    type RefType;
12    type FfiType;
13}
14
15#[macro_export]
16macro_rules! mini_object_wrapper (
17    ($name:ident, $ref_name:ident, $ffi_name:path) => {
18        #[repr(transparent)]
19        pub struct $name {
20            obj: std::ptr::NonNull<$ffi_name>,
21        }
22
23        #[repr(transparent)]
24        pub struct $ref_name($ffi_name);
25
26        impl $crate::miniobject::IsMiniObject for $name {
27            type RefType = $ref_name;
28            type FfiType = $ffi_name;
29        }
30
31        impl $name {
32            #[inline]
33            pub unsafe fn from_glib_ptr_borrow(
34                ptr: &*mut $ffi_name,
35            ) -> &Self { unsafe {
36                debug_assert_eq!(std::mem::size_of::<$name>(), std::mem::size_of::<$crate::glib::ffi::gpointer>());
37                debug_assert!(!ptr.is_null());
38                &*(ptr as *const *mut $ffi_name as *const $name)
39            }}
40
41            #[inline]
42            pub unsafe fn from_glib_none(ptr: *const $ffi_name) -> Self { unsafe {
43                skip_assert_initialized!();
44                debug_assert!(!ptr.is_null());
45
46                $crate::ffi::gst_mini_object_ref(ptr as *mut $crate::ffi::GstMiniObject);
47
48                $name {
49                    obj: std::ptr::NonNull::new_unchecked(ptr as *mut $ffi_name),
50                }
51            }}
52
53            #[inline]
54            pub unsafe fn from_glib_full(ptr: *const $ffi_name) -> Self { unsafe {
55                skip_assert_initialized!();
56                debug_assert!(!ptr.is_null());
57
58                $name {
59                    obj: std::ptr::NonNull::new_unchecked(ptr as *mut $ffi_name),
60                }
61            }}
62
63            #[inline]
64            pub unsafe fn from_glib_borrow(ptr: *const $ffi_name) -> $crate::glib::translate::Borrowed<Self> { unsafe {
65                skip_assert_initialized!();
66                debug_assert!(!ptr.is_null());
67
68                $crate::glib::translate::Borrowed::new($name {
69                    obj: std::ptr::NonNull::new_unchecked(ptr as *mut $ffi_name),
70                })
71            }}
72
73            #[inline]
74            pub unsafe fn replace_ptr(&mut self, ptr: *mut $ffi_name) { unsafe {
75                debug_assert!(!ptr.is_null());
76                self.obj = std::ptr::NonNull::new_unchecked(ptr);
77            }}
78
79            #[inline]
80            #[doc(alias = "gst_mini_object_make_writable")]
81            pub fn make_mut(&mut self) -> &mut $ref_name {
82                unsafe {
83                    if self.is_writable() {
84                        return &mut *(self.obj.as_mut() as *mut $ffi_name as *mut $ref_name);
85                    }
86
87                    let ptr = $crate::ffi::gst_mini_object_make_writable(
88                        self.as_mut_ptr() as *mut $crate::ffi::GstMiniObject
89                    );
90                    self.replace_ptr(ptr as *mut $ffi_name);
91                    debug_assert!(self.is_writable());
92
93                    &mut *(self.obj.as_mut() as *mut $ffi_name as *mut $ref_name)
94                }
95            }
96
97            #[inline]
98            pub fn get_mut(&mut self) -> Option<&mut $ref_name> {
99                if self.is_writable() {
100                    Some(unsafe { &mut *(self.obj.as_mut() as *mut $ffi_name as *mut $ref_name) })
101                } else {
102                    None
103                }
104            }
105
106            #[doc(alias = "gst_mini_object_is_writable")]
107            #[inline]
108            pub fn is_writable(&self) -> bool {
109                unsafe {
110                    $crate::glib::translate::from_glib($crate::ffi::gst_mini_object_is_writable(
111                        self.as_ptr() as *const $crate::ffi::GstMiniObject
112                    ))
113                }
114            }
115
116            #[must_use]
117            #[inline]
118            pub fn upcast(self) -> $crate::miniobject::MiniObject {
119                use $crate::glib::translate::IntoGlibPtr;
120
121                unsafe {
122                    from_glib_full(self.into_glib_ptr() as *mut $crate::ffi::GstMiniObject)
123                }
124            }
125        }
126
127        impl $crate::glib::translate::IntoGlibPtr<*mut $ffi_name> for $name {
128            #[inline]
129            fn into_glib_ptr(self) -> *mut $ffi_name {
130                let s = std::mem::ManuallyDrop::new(self);
131                s.as_mut_ptr()
132            }
133        }
134
135        impl Clone for $name {
136            #[inline]
137            fn clone(&self) -> Self {
138                unsafe { $name::from_glib_none(self.as_ptr()) }
139            }
140        }
141
142        impl Drop for $name {
143            #[inline]
144            fn drop(&mut self) {
145                unsafe {
146                    $crate::ffi::gst_mini_object_unref(self.as_mut_ptr() as *mut $crate::ffi::GstMiniObject);
147                }
148            }
149        }
150
151        impl std::ops::Deref for $name {
152            type Target = $ref_name;
153
154            #[inline]
155            fn deref(&self) -> &Self::Target {
156                unsafe { &*(self.obj.as_ref() as *const $ffi_name as *const $ref_name) }
157            }
158        }
159
160        impl AsRef<$ref_name> for $name {
161            #[inline]
162            fn as_ref(&self) -> &$ref_name {
163                &*self
164            }
165        }
166
167        impl std::borrow::Borrow<$ref_name> for $name {
168            #[inline]
169            fn borrow(&self) -> &$ref_name {
170                &*self
171            }
172        }
173
174        impl<'a> $crate::glib::translate::ToGlibPtr<'a, *const $ffi_name> for $name {
175            type Storage = std::marker::PhantomData<&'a Self>;
176
177            #[inline]
178            fn to_glib_none(&'a self) -> $crate::glib::translate::Stash<'a, *const $ffi_name, Self> {
179                $crate::glib::translate::Stash(self.as_ptr(), std::marker::PhantomData)
180            }
181
182            #[inline]
183            fn to_glib_full(&self) -> *const $ffi_name {
184                unsafe {
185                    $crate::ffi::gst_mini_object_ref(self.as_mut_ptr() as *mut $crate::ffi::GstMiniObject);
186                    self.as_ptr()
187                }
188            }
189        }
190
191        impl<'a> $crate::glib::translate::ToGlibPtr<'a, *mut $ffi_name> for $name {
192            type Storage = std::marker::PhantomData<&'a Self>;
193
194            #[inline]
195            fn to_glib_none(&'a self) -> $crate::glib::translate::Stash<'a, *mut $ffi_name, Self> {
196                $crate::glib::translate::Stash(self.as_mut_ptr(), std::marker::PhantomData)
197            }
198
199            #[inline]
200            fn to_glib_full(&self) -> *mut $ffi_name {
201                unsafe {
202                    $crate::ffi::gst_mini_object_ref(self.as_mut_ptr() as *mut $crate::ffi::GstMiniObject);
203                    self.as_mut_ptr()
204                }
205            }
206        }
207
208        impl<'a> $crate::glib::translate::ToGlibPtrMut<'a, *mut $ffi_name> for $name {
209            type Storage = std::marker::PhantomData<&'a mut Self>;
210
211            #[inline]
212            fn to_glib_none_mut(&'_ mut self) -> $crate::glib::translate::StashMut<'_, *mut $ffi_name, Self> {
213                self.make_mut();
214                $crate::glib::translate::StashMut(self.as_mut_ptr(), std::marker::PhantomData)
215            }
216        }
217
218        impl<'a> $crate::glib::translate::ToGlibContainerFromSlice<'a, *mut *mut $ffi_name> for $name {
219            #[allow(clippy::type_complexity)]
220            type Storage = (
221                std::marker::PhantomData<&'a [$name]>,
222                Option<Vec<*mut $ffi_name>>,
223            );
224
225            fn to_glib_none_from_slice(t: &'a [$name]) -> (*mut *mut $ffi_name, Self::Storage) {
226                skip_assert_initialized!();
227                let mut v_ptr = Vec::with_capacity(t.len() + 1);
228                unsafe {
229                    let ptr = v_ptr.as_mut_ptr();
230                    std::ptr::copy_nonoverlapping(t.as_ptr() as *mut *mut $ffi_name, ptr, t.len());
231                    std::ptr::write(ptr.add(t.len()), std::ptr::null_mut());
232                    v_ptr.set_len(t.len() + 1);
233                }
234
235                (v_ptr.as_ptr() as *mut *mut $ffi_name, (std::marker::PhantomData, Some(v_ptr)))
236            }
237
238            fn to_glib_container_from_slice(t: &'a [$name]) -> (*mut *mut $ffi_name, Self::Storage) {
239                skip_assert_initialized!();
240
241                let v_ptr = unsafe {
242                    let v_ptr = $crate::glib::ffi::g_malloc(std::mem::size_of::<*mut $ffi_name>() * t.len() + 1)
243                        as *mut *mut $ffi_name;
244
245                    std::ptr::copy_nonoverlapping(t.as_ptr() as *mut *mut $ffi_name, v_ptr, t.len());
246                    std::ptr::write(v_ptr.add(t.len()), std::ptr::null_mut());
247
248                    v_ptr
249                };
250
251                (v_ptr, (std::marker::PhantomData, None))
252            }
253
254            fn to_glib_full_from_slice(t: &[$name]) -> *mut *mut $ffi_name {
255                skip_assert_initialized!();
256                unsafe {
257                    let v_ptr = $crate::glib::ffi::g_malloc(std::mem::size_of::<*mut $ffi_name>() * t.len() + 1)
258                        as *mut *mut $ffi_name;
259
260                    for (i, s) in t.iter().enumerate() {
261                        std::ptr::write(v_ptr.add(i), $crate::glib::translate::ToGlibPtr::to_glib_full(s));
262                    }
263                    std::ptr::write(v_ptr.add(t.len()), std::ptr::null_mut());
264
265                    v_ptr
266                }
267            }
268        }
269
270        impl<'a> $crate::glib::translate::ToGlibContainerFromSlice<'a, *const *mut $ffi_name>
271            for $name
272        {
273            #[allow(clippy::type_complexity)]
274            type Storage = (
275                std::marker::PhantomData<&'a [$name]>,
276                Option<Vec<*mut $ffi_name>>,
277            );
278
279            fn to_glib_none_from_slice(t: &'a [$name]) -> (*const *mut $ffi_name, Self::Storage) {
280                skip_assert_initialized!();
281                let (ptr, stash) =
282                    $crate::glib::translate::ToGlibContainerFromSlice::<'a, *mut *mut $ffi_name>::to_glib_none_from_slice(t);
283                (ptr as *const *mut $ffi_name, stash)
284            }
285
286            fn to_glib_container_from_slice(_: &'a [$name]) -> (*const *mut $ffi_name, Self::Storage) {
287                skip_assert_initialized!();
288                // Can't have consumer free a *const pointer
289                unimplemented!()
290            }
291
292            fn to_glib_full_from_slice(_: &[$name]) -> *const *mut $ffi_name {
293                skip_assert_initialized!();
294                // Can't have consumer free a *const pointer
295                unimplemented!()
296            }
297        }
298
299        impl $crate::glib::translate::FromGlibPtrNone<*const $ffi_name> for $name {
300            #[inline]
301            unsafe fn from_glib_none(ptr: *const $ffi_name) -> Self { unsafe {
302                Self::from_glib_none(ptr)
303            }}
304        }
305
306        impl $crate::glib::translate::FromGlibPtrNone<*mut $ffi_name> for $name {
307            #[inline]
308            unsafe fn from_glib_none(ptr: *mut $ffi_name) -> Self { unsafe {
309                Self::from_glib_none(ptr)
310            }}
311        }
312
313        impl $crate::glib::translate::FromGlibPtrFull<*const $ffi_name> for $name {
314            #[inline]
315            unsafe fn from_glib_full(ptr: *const $ffi_name) -> Self { unsafe {
316                Self::from_glib_full(ptr)
317            }}
318        }
319
320        impl $crate::glib::translate::FromGlibPtrFull<*mut $ffi_name> for $name {
321            #[inline]
322            unsafe fn from_glib_full(ptr: *mut $ffi_name) -> Self { unsafe {
323                Self::from_glib_full(ptr)
324            }}
325        }
326
327        impl $crate::glib::translate::FromGlibPtrBorrow<*const $ffi_name> for $name {
328            #[inline]
329            unsafe fn from_glib_borrow(ptr: *const $ffi_name) -> $crate::glib::translate::Borrowed<Self> { unsafe {
330                Self::from_glib_borrow(ptr)
331            }}
332        }
333
334        impl $crate::glib::translate::FromGlibPtrBorrow<*mut $ffi_name> for $name {
335            #[inline]
336            unsafe fn from_glib_borrow(ptr: *mut $ffi_name) -> $crate::glib::translate::Borrowed<Self> { unsafe {
337                Self::from_glib_borrow(ptr)
338            }}
339        }
340
341        impl $crate::glib::translate::FromGlibContainerAsVec<*mut $ffi_name, *mut *mut $ffi_name>
342            for $name
343        {
344            unsafe fn from_glib_none_num_as_vec(ptr: *mut *mut $ffi_name, num: usize) -> Vec<Self> { unsafe {
345                if num == 0 || ptr.is_null() {
346                    return Vec::new();
347                }
348
349                let mut res = Vec::<Self>::with_capacity(num);
350                let res_ptr = res.as_mut_ptr();
351                for i in 0..num {
352                    ::std::ptr::write(res_ptr.add(i), $crate::glib::translate::from_glib_none(std::ptr::read(ptr.add(i))));
353                }
354                res.set_len(num);
355                res
356            }}
357
358            unsafe fn from_glib_container_num_as_vec(ptr: *mut *mut $ffi_name, num: usize) -> Vec<Self> { unsafe {
359                let res = $crate::glib::translate::FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, num);
360                $crate::glib::ffi::g_free(ptr as *mut _);
361                res
362            }}
363
364            unsafe fn from_glib_full_num_as_vec(ptr: *mut *mut $ffi_name, num: usize) -> Vec<Self> { unsafe {
365                if num == 0 || ptr.is_null() {
366                    return Vec::new();
367                }
368
369                let mut res = Vec::with_capacity(num);
370                let res_ptr = res.as_mut_ptr();
371                ::std::ptr::copy_nonoverlapping(ptr as *mut Self, res_ptr, num);
372                res.set_len(num);
373                $crate::glib::ffi::g_free(ptr as *mut _);
374                res
375            }}
376        }
377
378        impl $crate::glib::translate::FromGlibPtrArrayContainerAsVec<*mut $ffi_name, *mut *mut $ffi_name>
379            for $name
380        {
381            unsafe fn from_glib_none_as_vec(ptr: *mut *mut $ffi_name) -> Vec<Self> { unsafe {
382                $crate::glib::translate::FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr, glib::translate::c_ptr_array_len(ptr))
383            }}
384
385            unsafe fn from_glib_container_as_vec(ptr: *mut *mut $ffi_name) -> Vec<Self> { unsafe {
386                $crate::glib::translate::FromGlibContainerAsVec::from_glib_container_num_as_vec(ptr, glib::translate::c_ptr_array_len(ptr))
387            }}
388
389            unsafe fn from_glib_full_as_vec(ptr: *mut *mut $ffi_name) -> Vec<Self> { unsafe {
390                $crate::glib::translate::FromGlibContainerAsVec::from_glib_full_num_as_vec(ptr, glib::translate::c_ptr_array_len(ptr))
391            }}
392        }
393
394        impl $crate::glib::translate::FromGlibContainerAsVec<*mut $ffi_name, *const *mut $ffi_name>
395            for $name
396        {
397            unsafe fn from_glib_none_num_as_vec(ptr: *const *mut $ffi_name, num: usize) -> Vec<Self> { unsafe {
398                $crate::glib::translate::FromGlibContainerAsVec::from_glib_none_num_as_vec(ptr as *mut *mut _, num)
399            }}
400
401            unsafe fn from_glib_container_num_as_vec(_: *const *mut $ffi_name, _: usize) -> Vec<Self> {
402                // Can't free a *const
403                unimplemented!()
404            }
405
406            unsafe fn from_glib_full_num_as_vec(_: *const *mut $ffi_name, _: usize) -> Vec<Self> {
407                // Can't free a *const
408                unimplemented!()
409            }
410        }
411
412        impl $crate::glib::translate::FromGlibPtrArrayContainerAsVec<*mut $ffi_name, *const *mut $ffi_name> for $name
413        {
414            unsafe fn from_glib_none_as_vec(ptr: *const *mut $ffi_name) -> Vec<Self> { unsafe {
415                $crate::glib::translate::FromGlibPtrArrayContainerAsVec::from_glib_none_as_vec(ptr as *mut *mut _)
416            }}
417
418            unsafe fn from_glib_container_as_vec(_: *const *mut $ffi_name) -> Vec<Self> {
419                // Can't free a *const
420                unimplemented!()
421            }
422
423            unsafe fn from_glib_full_as_vec(_: *const *mut $ffi_name) -> Vec<Self> {
424                // Can't free a *const
425                unimplemented!()
426            }
427        }
428
429        impl $crate::glib::translate::GlibPtrDefault for $name {
430            type GlibType = *mut $ffi_name;
431        }
432
433        unsafe impl $crate::glib::translate::TransparentPtrType for $name {}
434
435        impl $ref_name {
436            #[inline]
437            pub fn as_ptr(&self) -> *const $ffi_name {
438                self as *const Self as *const $ffi_name
439            }
440
441            #[inline]
442            pub fn as_mut_ptr(&self) -> *mut $ffi_name {
443                self as *const Self as *mut $ffi_name
444            }
445
446            #[inline]
447            pub unsafe fn from_ptr<'a>(ptr: *const $ffi_name) -> &'a Self { unsafe {
448                debug_assert!(!ptr.is_null());
449                &*(ptr as *const Self)
450            }}
451
452            #[inline]
453            pub unsafe fn from_mut_ptr<'a>(ptr: *mut $ffi_name) -> &'a mut Self { unsafe {
454                debug_assert!(!ptr.is_null());
455                debug_assert_ne!(
456                    $crate::ffi::gst_mini_object_is_writable(ptr as *mut $crate::ffi::GstMiniObject),
457                    $crate::glib::ffi::GFALSE
458                );
459                &mut *(ptr as *mut Self)
460            }}
461
462            #[doc(alias = "gst_mini_object_copy")]
463            #[inline]
464            pub fn copy(&self) -> $name {
465                unsafe {
466                    $name::from_glib_full($crate::ffi::gst_mini_object_copy(
467                        self.as_ptr() as *const $crate::ffi::GstMiniObject
468                    ) as *const $ffi_name)
469                }
470            }
471
472            #[inline]
473            pub fn upcast_ref(&self) -> &$crate::miniobject::MiniObjectRef {
474                unsafe {
475                    &*(self.as_ptr() as *const $crate::miniobject::MiniObjectRef)
476                }
477            }
478
479            #[inline]
480            pub fn upcast_mut(&mut self) -> &mut $crate::miniobject::MiniObjectRef {
481                unsafe {
482                    &mut *(self.as_mut_ptr() as *mut $crate::miniobject::MiniObjectRef)
483                }
484            }
485
486            #[inline]
487            pub fn ptr_eq(this: &$ref_name, other: &$ref_name) -> bool {
488                skip_assert_initialized!();
489                this.as_ptr() == other.as_ptr()
490            }
491
492            #[inline]
493            pub fn mark_may_be_leaked(&mut self) {
494                unsafe {
495                    (*(self.as_mut_ptr() as *mut $crate::ffi::GstMiniObject)).flags
496                        |= $crate::ffi::GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED;
497                }
498            }
499        }
500
501        impl $crate::glib::translate::GlibPtrDefault for $ref_name {
502            type GlibType = *mut $ffi_name;
503        }
504
505        impl ToOwned for $ref_name {
506            type Owned = $name;
507
508            #[inline]
509            fn to_owned(&self) -> $name {
510                self.copy()
511            }
512        }
513
514        unsafe impl Sync for $ref_name {}
515        unsafe impl Send for $ref_name {}
516        unsafe impl Sync for $name {}
517        unsafe impl Send for $name {}
518    };
519    ($name:ident, $ref_name:ident, $ffi_name:path, $get_type:expr) => {
520        $crate::mini_object_wrapper!($name, $ref_name, $ffi_name);
521
522        impl $crate::glib::types::StaticType for $name {
523            #[inline]
524            fn static_type() -> $crate::glib::types::Type {
525                $ref_name::static_type()
526            }
527        }
528
529        #[allow(clippy::redundant_closure_call)]
530        impl $crate::glib::types::StaticType for $ref_name {
531            #[inline]
532            fn static_type() -> $crate::glib::types::Type {
533                #[allow(clippy::macro_metavars_in_unsafe)]
534                unsafe { $crate::glib::translate::from_glib($get_type()) }
535            }
536        }
537
538        impl glib::value::ValueType for $name {
539            type Type = Self;
540        }
541
542        impl glib::value::ValueTypeOptional for $name { }
543
544        unsafe impl<'a> $crate::glib::value::FromValue<'a> for $name {
545            type Checker = $crate::glib::value::GenericValueTypeOrNoneChecker<Self>;
546
547            #[inline]
548            unsafe fn from_value(value: &'a $crate::glib::Value) -> Self { unsafe {
549                skip_assert_initialized!();
550                $crate::glib::translate::from_glib_none(
551                    $crate::glib::gobject_ffi::g_value_get_boxed($crate::glib::translate::ToGlibPtr::to_glib_none(value).0) as *mut $ffi_name
552                )
553            }}
554        }
555
556        unsafe impl<'a> $crate::glib::value::FromValue<'a> for &'a $name {
557            type Checker = $crate::glib::value::GenericValueTypeOrNoneChecker<Self>;
558
559            #[inline]
560            unsafe fn from_value(value: &'a $crate::glib::Value) -> Self { unsafe {
561                skip_assert_initialized!();
562                let value = &*(value as *const $crate::glib::Value as *const $crate::glib::gobject_ffi::GValue);
563                $name::from_glib_ptr_borrow(&*(&value.data[0].v_pointer as *const $crate::glib::ffi::gpointer as *const *mut $ffi_name))
564            }}
565        }
566
567        impl $crate::glib::value::ToValue for $name {
568            #[inline]
569            fn to_value(&self) -> $crate::glib::Value {
570                let mut value = $crate::glib::Value::for_value_type::<Self>();
571                unsafe {
572                    $crate::glib::gobject_ffi::g_value_set_boxed(
573                        $crate::glib::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
574                        $crate::glib::translate::ToGlibPtr::<*const $ffi_name>::to_glib_none(self).0 as *mut _,
575                    )
576                }
577                value
578            }
579
580            #[inline]
581            fn value_type(&self) -> $crate::glib::Type {
582                <Self as $crate::glib::prelude::StaticType>::static_type()
583            }
584        }
585
586        impl $crate::glib::value::ToValueOptional for $name {
587            #[inline]
588            fn to_value_optional(s: Option<&Self>) -> $crate::glib::Value {
589                skip_assert_initialized!();
590                let mut value = $crate::glib::Value::for_value_type::<Self>();
591                unsafe {
592                    $crate::glib::gobject_ffi::g_value_set_boxed(
593                        $crate::glib::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
594                        $crate::glib::translate::ToGlibPtr::<*const $ffi_name>::to_glib_none(&s).0 as *mut _,
595                    )
596                }
597                value
598            }
599        }
600
601        impl From<$name> for $crate::glib::Value {
602            #[inline]
603            fn from(v: $name) -> $crate::glib::Value {
604                skip_assert_initialized!();
605                let mut value = $crate::glib::Value::for_value_type::<$name>();
606                unsafe {
607                    $crate::glib::gobject_ffi::g_value_take_boxed(
608                        $crate::glib::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
609                        $crate::glib::translate::IntoGlibPtr::<*mut $ffi_name>::into_glib_ptr(v) as *mut _,
610                    )
611                }
612                value
613            }
614        }
615
616        unsafe impl<'a> $crate::glib::value::FromValue<'a> for &'a $ref_name {
617            type Checker = $crate::glib::value::GenericValueTypeOrNoneChecker<Self>;
618
619            #[inline]
620            unsafe fn from_value(value: &'a $crate::glib::Value) -> Self { unsafe {
621                skip_assert_initialized!();
622                &*($crate::glib::gobject_ffi::g_value_get_boxed($crate::glib::translate::ToGlibPtr::to_glib_none(value).0) as *const $ref_name)
623            }}
624        }
625
626        // Can't have SetValue/SetValueOptional impls as otherwise one could use it to get
627        // immutable references from a mutable reference without borrowing via the value
628
629        impl $crate::glib::prelude::HasParamSpec for $name {
630            type ParamSpec = $crate::glib::ParamSpecBoxed;
631            type SetValue = Self;
632            type BuilderFn = fn(&str) -> $crate::glib::ParamSpecBoxedBuilder<Self>;
633
634            fn param_spec_builder() -> Self::BuilderFn {
635                |name| Self::ParamSpec::builder(name)
636            }
637        }
638    };
639);
640
641#[cfg(not(any(feature = "v1_20", docsrs)))]
642mini_object_wrapper!(MiniObject, MiniObjectRef, ffi::GstMiniObject);
643
644#[cfg(feature = "v1_20")]
645mini_object_wrapper!(MiniObject, MiniObjectRef, ffi::GstMiniObject, || {
646    ffi::gst_mini_object_get_type()
647});
648
649impl MiniObject {
650    #[inline]
651    pub fn downcast<T: IsMiniObject + StaticType>(self) -> Result<T, Self> {
652        if self.type_().is_a(T::static_type()) {
653            unsafe { Ok(from_glib_full(self.into_glib_ptr() as *mut T::FfiType)) }
654        } else {
655            Err(self)
656        }
657    }
658
659    #[inline]
660    pub fn downcast_ref<T: IsMiniObject + StaticType>(&self) -> Option<&T> {
661        if self.type_().is_a(T::static_type()) {
662            unsafe { Some(&*(self as *const Self as *const T)) }
663        } else {
664            None
665        }
666    }
667
668    #[inline]
669    pub fn downcast_mut<T: IsMiniObject + StaticType>(&mut self) -> Option<&mut T> {
670        if self.type_().is_a(T::static_type()) {
671            unsafe { Some(&mut *(self as *mut Self as *mut T)) }
672        } else {
673            None
674        }
675    }
676}
677
678impl fmt::Debug for MiniObject {
679    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
680        self.as_ref().fmt(f)
681    }
682}
683
684impl fmt::Debug for MiniObjectRef {
685    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
686        f.debug_struct("MiniObject")
687            .field("ptr", &self.as_ptr())
688            .field("type", &self.type_())
689            .finish()
690    }
691}
692
693impl MiniObjectRef {
694    #[inline]
695    pub fn type_(&self) -> glib::Type {
696        unsafe { from_glib((*self.as_ptr()).type_) }
697    }
698
699    #[inline]
700    pub fn downcast_ref<T: IsMiniObject + StaticType>(&self) -> Option<&T::RefType> {
701        if self.type_().is_a(T::static_type()) {
702            unsafe { Some(&*(self as *const Self as *const T::RefType)) }
703        } else {
704            None
705        }
706    }
707
708    #[inline]
709    pub fn downcast_mut<T: IsMiniObject + StaticType>(&mut self) -> Option<&mut T::RefType> {
710        if self.type_().is_a(T::static_type()) {
711            unsafe { Some(&mut *(self as *mut Self as *mut T::RefType)) }
712        } else {
713            None
714        }
715    }
716}