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
// 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 {
    #[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));
                    }),
                );
            },
        ))
    }
}