[go: up one dir, main page]

gio/auto/
async_initable.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ffi, AsyncResult, Cancellable};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10    #[doc(alias = "GAsyncInitable")]
11    pub struct AsyncInitable(Interface<ffi::GAsyncInitable, ffi::GAsyncInitableIface>);
12
13    match fn {
14        type_ => || ffi::g_async_initable_get_type(),
15    }
16}
17
18impl AsyncInitable {
19    pub const NONE: Option<&'static AsyncInitable> = None;
20}
21
22pub trait AsyncInitableExt: IsA<AsyncInitable> + 'static {
23    #[doc(alias = "g_async_initable_init_async")]
24    unsafe fn init_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
25        &self,
26        io_priority: glib::Priority,
27        cancellable: Option<&impl IsA<Cancellable>>,
28        callback: P,
29    ) {
30        let main_context = glib::MainContext::ref_thread_default();
31        let is_main_context_owner = main_context.is_owner();
32        let has_acquired_main_context = (!is_main_context_owner)
33            .then(|| main_context.acquire().ok())
34            .flatten();
35        assert!(
36            is_main_context_owner || has_acquired_main_context.is_some(),
37            "Async operations only allowed if the thread is owning the MainContext"
38        );
39
40        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
41            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
42        unsafe extern "C" fn init_async_trampoline<P: FnOnce(Result<(), glib::Error>) + 'static>(
43            _source_object: *mut glib::gobject_ffi::GObject,
44            res: *mut crate::ffi::GAsyncResult,
45            user_data: glib::ffi::gpointer,
46        ) {
47            let mut error = std::ptr::null_mut();
48            ffi::g_async_initable_init_finish(_source_object as *mut _, res, &mut error);
49            let result = if error.is_null() {
50                Ok(())
51            } else {
52                Err(from_glib_full(error))
53            };
54            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
55                Box_::from_raw(user_data as *mut _);
56            let callback: P = callback.into_inner();
57            callback(result);
58        }
59        let callback = init_async_trampoline::<P>;
60        ffi::g_async_initable_init_async(
61            self.as_ref().to_glib_none().0,
62            io_priority.into_glib(),
63            cancellable.map(|p| p.as_ref()).to_glib_none().0,
64            Some(callback),
65            Box_::into_raw(user_data) as *mut _,
66        );
67    }
68
69    unsafe fn init_future(
70        &self,
71        io_priority: glib::Priority,
72    ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
73        Box_::pin(crate::GioFuture::new(
74            self,
75            move |obj, cancellable, send| {
76                obj.init_async(io_priority, Some(cancellable), move |res| {
77                    send.resolve(res);
78                });
79            },
80        ))
81    }
82}
83
84impl<O: IsA<AsyncInitable>> AsyncInitableExt for O {}