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
93
94
95
96
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::traits::InitableExt;
use crate::Cancellable;
use crate::Initable;
use glib::object::IsA;
use glib::object::IsClass;
use glib::value::ToValue;
use glib::{Cast, Object, StaticType, Type};

impl Initable {
    // rustdoc-stripper-ignore-next
    /// Create a new instance of an initable object with the given properties.
    ///
    /// Similar to [`Object::new`] but can fail because the object initialization in
    /// `Initable::init` failed.
    // rustdoc-stripper-ignore-next-stop
    /// Helper function for constructing [`Initable`][crate::Initable] object. This is
    /// similar to [`glib::Object::new()`][crate::glib::Object::new()] but also initializes the object
    /// and returns [`None`], setting an error on failure.
    /// ## `object_type`
    /// a `GType` supporting [`Initable`][crate::Initable].
    /// ## `cancellable`
    /// optional [`Cancellable`][crate::Cancellable] object, [`None`] to ignore.
    /// ## `first_property_name`
    /// the name of the first property, or [`None`] if no
    ///  properties
    ///
    /// # Returns
    ///
    /// a newly allocated
    ///  [`glib::Object`][crate::glib::Object], or [`None`] on error
    #[allow(clippy::new_ret_no_self)]
    #[track_caller]
    pub fn new<O: Sized + IsClass + IsA<Object> + IsA<Initable>, P: IsA<Cancellable>>(
        properties: &[(&str, &dyn ToValue)],
        cancellable: Option<&P>,
    ) -> Result<O, glib::Error> {
        Self::with_type(O::static_type(), properties, cancellable)
            .map(|o| unsafe { o.unsafe_cast() })
    }

    // rustdoc-stripper-ignore-next
    /// Create a new instance of an initable object of the given type with the given properties.
    ///
    /// Similar to [`Object::with_type`] but can fail because the object initialization in
    /// `Initable::init` failed.
    #[track_caller]
    pub fn with_type(
        type_: Type,
        properties: &[(&str, &dyn ToValue)],
        cancellable: Option<&impl IsA<Cancellable>>,
    ) -> Result<Object, glib::Error> {
        if !type_.is_a(Initable::static_type()) {
            panic!("Type '{type_}' is not initable");
        }

        let mut property_values = smallvec::SmallVec::<[_; 16]>::with_capacity(properties.len());
        for (name, value) in properties {
            property_values.push((*name, value.to_value()));
        }

        unsafe {
            let object = Object::new_internal(type_, &mut property_values);
            object.unsafe_cast_ref::<Self>().init(cancellable)?;
            Ok(object)
        }
    }

    // rustdoc-stripper-ignore-next
    /// Create a new instance of an initable object of the given type with the given properties.
    ///
    /// Similar to [`Object::with_values`] but can fail because the object initialization in
    /// `Initable::init` failed.
    #[track_caller]
    pub fn with_values(
        type_: Type,
        properties: &[(&str, glib::Value)],
        cancellable: Option<&impl IsA<Cancellable>>,
    ) -> Result<Object, glib::Error> {
        if !type_.is_a(Initable::static_type()) {
            panic!("Type '{type_}' is not initable");
        }

        let mut property_values = smallvec::SmallVec::<[_; 16]>::with_capacity(properties.len());
        for (name, value) in properties {
            property_values.push((*name, value.clone()));
        }

        unsafe {
            let object = Object::new_internal(type_, &mut property_values);
            object.unsafe_cast_ref::<Self>().init(cancellable)?;
            Ok(object)
        }
    }
}