gstreamer/
gtype.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::ffi::c_void;
4
5use crate::ffi;
6use glib::translate::*;
7
8pub trait PluginApiExt {
9    #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
10    #[doc(alias = "gst_type_mark_as_plugin_api")]
11    fn mark_as_plugin_api(self, flags: crate::PluginAPIFlags);
12    #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
13    #[doc(alias = "gst_type_is_plugin_api")]
14    fn plugin_api_flags(self) -> Option<crate::PluginAPIFlags>;
15    #[doc(alias = "gst_element_type_set_skip_documentation")]
16    fn set_skip_documentation(self);
17    #[doc(alias = "gst_element_factory_get_skip_documentation")]
18    fn skip_documentation(self) -> bool;
19}
20
21impl PluginApiExt for glib::Type {
22    fn set_skip_documentation(self) {
23        let quark = glib::Quark::from_str("GST_ELEMENTCLASS_SKIP_DOCUMENTATION");
24        #[allow(clippy::manual_dangling_ptr)]
25        unsafe {
26            crate::glib::gobject_ffi::g_type_set_qdata(
27                self.into_glib(),
28                quark.into_glib(),
29                1 as *mut c_void,
30            );
31        }
32    }
33
34    fn skip_documentation(self) -> bool {
35        let quark = glib::Quark::from_str("GST_ELEMENTCLASS_SKIP_DOCUMENTATION");
36        unsafe {
37            !crate::glib::gobject_ffi::g_type_get_qdata(self.into_glib(), quark.into_glib())
38                .is_null()
39        }
40    }
41
42    fn plugin_api_flags(self) -> Option<crate::PluginAPIFlags> {
43        assert_initialized_main_thread!();
44        unsafe {
45            use std::mem;
46
47            let mut flags = mem::MaybeUninit::uninit();
48            let ret = from_glib(ffi::gst_type_is_plugin_api(
49                self.into_glib(),
50                flags.as_mut_ptr(),
51            ));
52            if ret {
53                Some(from_glib(flags.assume_init()))
54            } else {
55                None
56            }
57        }
58    }
59
60    fn mark_as_plugin_api(self, flags: crate::PluginAPIFlags) {
61        assert_initialized_main_thread!();
62
63        unsafe { ffi::gst_type_mark_as_plugin_api(self.into_glib(), flags.into_glib()) }
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use glib::prelude::StaticType;
70
71    use super::*;
72
73    #[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy, glib::Enum)]
74    #[repr(u32)]
75    #[enum_type(name = "GstTestEnum")]
76    pub enum TestEnum {
77        #[enum_value(name = "test", nick = "test")]
78        Test,
79    }
80
81    #[test]
82    fn test_gtype_mark_as_api() {
83        crate::init().unwrap();
84
85        assert!(TestEnum::static_type().plugin_api_flags().is_none());
86        assert!(!TestEnum::static_type().skip_documentation());
87
88        TestEnum::static_type().mark_as_plugin_api(crate::PluginAPIFlags::empty());
89        TestEnum::static_type().set_skip_documentation();
90
91        assert!(
92            TestEnum::static_type().plugin_api_flags().unwrap() == crate::PluginAPIFlags::empty()
93        );
94        assert!(TestEnum::static_type().skip_documentation());
95    }
96}