1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Take a look at the license at the top of the repository in the LICENSE file.

use glib::{
    prelude::*,
    translate::{from_glib, FromGlibPtrFull, IntoGlib, ToGlibPtr},
};

use crate::{ffi, PluginFeature, Rank};

mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::PluginFeature>> Sealed for T {}
}

pub trait PluginFeatureExtManual: sealed::Sealed + IsA<PluginFeature> + Sized + 'static {
    /// Gets the rank of a plugin feature.
    ///
    /// # Returns
    ///
    /// The rank of the feature
    #[doc(alias = "get_rank")]
    #[doc(alias = "gst_plugin_feature_get_rank")]
    fn rank(&self) -> Rank {
        unsafe {
            let rank = ffi::gst_plugin_feature_get_rank(self.as_ref().to_glib_none().0);
            from_glib(rank as i32)
        }
    }

    /// Specifies a rank for a plugin feature, so that autoplugging uses
    /// the most appropriate feature.
    /// ## `rank`
    /// rank value - higher number means more priority rank
    #[doc(alias = "gst_plugin_feature_set_rank")]
    fn set_rank(&self, rank: Rank) {
        unsafe {
            ffi::gst_plugin_feature_set_rank(
                self.as_ref().to_glib_none().0,
                rank.into_glib() as u32,
            );
        }
    }

    /// Loads the plugin containing `self` if it's not already loaded. `self` is
    /// unaffected; use the return value instead.
    ///
    /// Normally this function is used like this:
    ///
    ///
    /// **⚠️ The following code is in C ⚠️**
    ///
    /// ```C
    /// GstPluginFeature *loaded_feature;
    ///
    /// loaded_feature = gst_plugin_feature_load (feature);
    /// // presumably, we're no longer interested in the potentially-unloaded feature
    /// gst_object_unref (feature);
    /// feature = loaded_feature;
    /// ```
    ///
    /// # Returns
    ///
    /// a reference to the loaded
    /// feature, or [`None`] on error
    #[doc(alias = "gst_plugin_feature_load")]
    fn load(&self) -> Result<Self, glib::BoolError> {
        unsafe {
            let loaded = Option::<PluginFeature>::from_glib_full(ffi::gst_plugin_feature_load(
                self.as_ref().to_glib_none().0,
            ))
            .ok_or_else(|| glib::bool_error!("Failed to load plugin feature"))?;
            Ok(loaded.unsafe_cast())
        }
    }
}

impl<O: IsA<PluginFeature>> PluginFeatureExtManual for O {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_load() {
        crate::init().unwrap();

        let factory = crate::ElementFactory::find("identity").unwrap();
        let loaded = factory.load().unwrap();
        assert_eq!(factory.type_(), loaded.type_());
        let _element = loaded.create().build().unwrap();
    }
}