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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::traits::AsyncInitableExt;
use crate::AsyncInitable;
use crate::Cancellable;
use glib::object::IsA;
use glib::object::IsClass;
use glib::value::ToValue;
use glib::{Cast, Object, StaticType, Type};
use std::boxed::Box as Box_;
use std::pin::Pin;

impl AsyncInitable {
    /// Helper function for constructing [`AsyncInitable`][crate::AsyncInitable] object. This is
    /// similar to [`glib::Object::new()`][crate::glib::Object::new()] but also initializes the object asynchronously.
    ///
    /// When the initialization is finished, `callback` will be called. You can
    /// then call `g_async_initable_new_finish()` to get the new object and check
    /// for any errors.
    /// ## `object_type`
    /// a `GType` supporting [`AsyncInitable`][crate::AsyncInitable].
    /// ## `io_priority`
    /// the [I/O priority][io-priority] of the operation
    /// ## `cancellable`
    /// optional [`Cancellable`][crate::Cancellable] object, [`None`] to ignore.
    /// ## `callback`
    /// a `GAsyncReadyCallback` to call when the initialization is
    ///  finished
    /// ## `first_property_name`
    /// the name of the first property, or [`None`] if no
    ///  properties
    #[doc(alias = "g_async_initable_new_async")]
    pub fn new_async<
        O: Sized + IsClass + IsA<Object> + IsA<AsyncInitable>,
        P: IsA<Cancellable>,
        Q: FnOnce(Result<O, glib::Error>) + 'static,
    >(
        properties: &[(&str, &dyn ToValue)],
        io_priority: glib::Priority,
        cancellable: Option<&P>,
        callback: Q,
    ) {
        let obj = Object::new::<O>(properties).unwrap();
        unsafe {
            obj.init_async(
                io_priority,
                cancellable,
                glib::clone!(@strong obj => move |res| callback(res.map(|_| obj))),
            );
        }
    }

    #[doc(alias = "g_async_initable_new_async")]
    pub fn new_future<O: Sized + IsClass + IsA<Object> + IsA<AsyncInitable>>(
        properties: &[(&str, &dyn ToValue)],
        io_priority: glib::Priority,
    ) -> Pin<Box_<dyn std::future::Future<Output = Result<O, glib::Error>> + 'static>> {
        Box_::pin(crate::GioFuture::new(
            &Object::new::<O>(properties).unwrap(),
            move |obj, cancellable, send| unsafe {
                obj.init_async(
                    io_priority,
                    Some(cancellable),
                    glib::clone!(@strong obj => move |res| {
                        send.resolve(res.map(|_| obj));
                    }),
                );
            },
        ))
    }

    #[doc(alias = "g_async_initable_new_async")]
    pub fn with_type<P: IsA<Cancellable>, Q: FnOnce(Result<Object, glib::Error>) + 'static>(
        type_: Type,
        properties: &[(&str, &dyn ToValue)],
        io_priority: glib::Priority,
        cancellable: Option<&P>,
        callback: Q,
    ) {
        assert!(type_.is_a(AsyncInitable::static_type()));
        let obj = Object::with_type(type_, properties).unwrap();
        unsafe {
            obj.unsafe_cast_ref::<Self>().init_async(
                io_priority,
                cancellable,
                glib::clone!(@strong obj => move |res| callback(res.map(|_| obj))),
            )
        };
    }

    #[doc(alias = "g_async_initable_new_async")]
    pub fn with_type_future(
        type_: Type,
        properties: &[(&str, &dyn ToValue)],
        io_priority: glib::Priority,
    ) -> Pin<Box_<dyn std::future::Future<Output = Result<Object, glib::Error>> + 'static>> {
        assert!(type_.is_a(AsyncInitable::static_type()));
        Box_::pin(crate::GioFuture::new(
            &Object::with_type(type_, properties).unwrap(),
            move |obj, cancellable, send| unsafe {
                obj.unsafe_cast_ref::<Self>().init_async(
                    io_priority,
                    Some(cancellable),
                    glib::clone!(@strong obj => move |res| {
                        send.resolve(res.map(|_| obj));
                    }),
                );
            },
        ))
    }
}