Skip to content

Commit bba02e3

Browse files
authored
Merge pull request #602 from pbor/async-initable
gio: add AsyncInitable
2 parents 6d59895 + c85972d commit bba02e3

File tree

21 files changed

+240
-16
lines changed

21 files changed

+240
-16
lines changed

gdk-pixbuf/src/auto/versions.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Generated by gir (https://github.com/gtk-rs/gir @ e0d8d8d645b1)
1+
Generated by gir (https://github.com/gtk-rs/gir @ a4ffdd5a1de1)
22
from gir-files (https://github.com/gtk-rs/gir-files @ 951202c4b7fd)

gdk-pixbuf/sys/versions.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Generated by gir (https://github.com/gtk-rs/gir @ e0d8d8d645b1)
1+
Generated by gir (https://github.com/gtk-rs/gir @ a4ffdd5a1de1)
22
from gir-files (https://github.com/gtk-rs/gir-files @ 951202c4b7fd)

gio/Gir.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,23 @@ status = "generate"
307307
name = "name"
308308
string_type = "os_string"
309309

310+
[[object]]
311+
name = "Gio.AsyncInitable"
312+
status = "generate"
313+
[[object.function]]
314+
name = "init_async"
315+
# Can be called only once
316+
unsafe = true
317+
[[object.function]]
318+
name = "new_async"
319+
manual = true
320+
[[object.function]]
321+
name = "new_valist_async"
322+
ignore = true
323+
[[object.function]]
324+
name = "newv_async"
325+
ignore = true
326+
310327
[[object]]
311328
name = "Gio.BufferedInputStream"
312329
status = "generate"

gio/src/async_initable.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Take a look at the license at the top of the repository in the LICENSE file.
2+
3+
use crate::traits::AsyncInitableExt;
4+
use crate::AsyncInitable;
5+
use crate::Cancellable;
6+
use glib::object::IsA;
7+
use glib::object::IsClass;
8+
use glib::value::ToValue;
9+
use glib::{Cast, Object, StaticType, Type};
10+
use std::boxed::Box as Box_;
11+
use std::pin::Pin;
12+
13+
impl AsyncInitable {
14+
#[doc(alias = "g_async_initable_new_async")]
15+
pub fn new_async<
16+
O: Sized + IsClass + IsA<Object> + IsA<AsyncInitable>,
17+
P: IsA<Cancellable>,
18+
Q: FnOnce(Result<O, glib::Error>) + 'static,
19+
>(
20+
properties: &[(&str, &dyn ToValue)],
21+
io_priority: glib::Priority,
22+
cancellable: Option<&P>,
23+
callback: Q,
24+
) {
25+
let obj = Object::new::<O>(properties).unwrap();
26+
unsafe {
27+
obj.init_async(
28+
io_priority,
29+
cancellable,
30+
glib::clone!(@strong obj => move |res| callback(res.map(|_| obj))),
31+
);
32+
}
33+
}
34+
35+
#[doc(alias = "g_async_initable_new_async")]
36+
pub fn new_future<O: Sized + IsClass + IsA<Object> + IsA<AsyncInitable>>(
37+
properties: &[(&str, &dyn ToValue)],
38+
io_priority: glib::Priority,
39+
) -> Pin<Box_<dyn std::future::Future<Output = Result<O, glib::Error>> + 'static>> {
40+
Box_::pin(crate::GioFuture::new(
41+
&Object::new::<O>(properties).unwrap(),
42+
move |obj, cancellable, send| unsafe {
43+
obj.init_async(
44+
io_priority,
45+
Some(cancellable),
46+
glib::clone!(@strong obj => move |res| {
47+
send.resolve(res.map(|_| obj));
48+
}),
49+
);
50+
},
51+
))
52+
}
53+
54+
#[doc(alias = "g_async_initable_new_async")]
55+
pub fn with_type<P: IsA<Cancellable>, Q: FnOnce(Result<Object, glib::Error>) + 'static>(
56+
type_: Type,
57+
properties: &[(&str, &dyn ToValue)],
58+
io_priority: glib::Priority,
59+
cancellable: Option<&P>,
60+
callback: Q,
61+
) {
62+
assert!(type_.is_a(AsyncInitable::static_type()));
63+
let obj = Object::with_type(type_, properties).unwrap();
64+
unsafe {
65+
obj.unsafe_cast_ref::<Self>().init_async(
66+
io_priority,
67+
cancellable,
68+
glib::clone!(@strong obj => move |res| callback(res.map(|_| obj))),
69+
)
70+
};
71+
}
72+
73+
#[doc(alias = "g_async_initable_new_async")]
74+
pub fn with_type_future(
75+
type_: Type,
76+
properties: &[(&str, &dyn ToValue)],
77+
io_priority: glib::Priority,
78+
) -> Pin<Box_<dyn std::future::Future<Output = Result<Object, glib::Error>> + 'static>> {
79+
assert!(type_.is_a(AsyncInitable::static_type()));
80+
Box_::pin(crate::GioFuture::new(
81+
&Object::with_type(type_, properties).unwrap(),
82+
move |obj, cancellable, send| unsafe {
83+
obj.unsafe_cast_ref::<Self>().init_async(
84+
io_priority,
85+
Some(cancellable),
86+
glib::clone!(@strong obj => move |res| {
87+
send.resolve(res.map(|_| obj));
88+
}),
89+
);
90+
},
91+
))
92+
}
93+
}

gio/src/auto/async_initable.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
5+
use crate::AsyncResult;
6+
use crate::Cancellable;
7+
use glib::object::IsA;
8+
use glib::translate::*;
9+
use std::boxed::Box as Box_;
10+
use std::fmt;
11+
use std::pin::Pin;
12+
use std::ptr;
13+
14+
glib::wrapper! {
15+
#[doc(alias = "GAsyncInitable")]
16+
pub struct AsyncInitable(Interface<ffi::GAsyncInitable, ffi::GAsyncInitableIface>);
17+
18+
match fn {
19+
type_ => || ffi::g_async_initable_get_type(),
20+
}
21+
}
22+
23+
impl AsyncInitable {
24+
pub const NONE: Option<&'static AsyncInitable> = None;
25+
}
26+
27+
pub trait AsyncInitableExt: 'static {
28+
#[doc(alias = "g_async_initable_init_async")]
29+
unsafe fn init_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
30+
&self,
31+
io_priority: glib::Priority,
32+
cancellable: Option<&impl IsA<Cancellable>>,
33+
callback: P,
34+
);
35+
36+
unsafe fn init_future(
37+
&self,
38+
io_priority: glib::Priority,
39+
) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>>;
40+
}
41+
42+
impl<O: IsA<AsyncInitable>> AsyncInitableExt for O {
43+
unsafe fn init_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
44+
&self,
45+
io_priority: glib::Priority,
46+
cancellable: Option<&impl IsA<Cancellable>>,
47+
callback: P,
48+
) {
49+
let main_context = glib::MainContext::ref_thread_default();
50+
let is_main_context_owner = main_context.is_owner();
51+
let has_acquired_main_context = (!is_main_context_owner)
52+
.then(|| main_context.acquire().ok())
53+
.flatten();
54+
assert!(
55+
is_main_context_owner || has_acquired_main_context.is_some(),
56+
"Async operations only allowed if the thread is owning the MainContext"
57+
);
58+
59+
let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
60+
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
61+
unsafe extern "C" fn init_async_trampoline<P: FnOnce(Result<(), glib::Error>) + 'static>(
62+
_source_object: *mut glib::gobject_ffi::GObject,
63+
res: *mut crate::ffi::GAsyncResult,
64+
user_data: glib::ffi::gpointer,
65+
) {
66+
let mut error = ptr::null_mut();
67+
let _ = ffi::g_async_initable_init_finish(_source_object as *mut _, res, &mut error);
68+
let result = if error.is_null() {
69+
Ok(())
70+
} else {
71+
Err(from_glib_full(error))
72+
};
73+
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
74+
Box_::from_raw(user_data as *mut _);
75+
let callback: P = callback.into_inner();
76+
callback(result);
77+
}
78+
let callback = init_async_trampoline::<P>;
79+
ffi::g_async_initable_init_async(
80+
self.as_ref().to_glib_none().0,
81+
io_priority.into_glib(),
82+
cancellable.map(|p| p.as_ref()).to_glib_none().0,
83+
Some(callback),
84+
Box_::into_raw(user_data) as *mut _,
85+
);
86+
}
87+
88+
unsafe fn init_future(
89+
&self,
90+
io_priority: glib::Priority,
91+
) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
92+
Box_::pin(crate::GioFuture::new(
93+
self,
94+
move |obj, cancellable, send| {
95+
obj.init_async(io_priority, Some(cancellable), move |res| {
96+
send.resolve(res);
97+
});
98+
},
99+
))
100+
}
101+
}
102+
103+
impl fmt::Display for AsyncInitable {
104+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105+
f.write_str("AsyncInitable")
106+
}
107+
}

gio/src/auto/dbus_connection.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// from gir-files (https://github.com/gtk-rs/gir-files)
33
// DO NOT EDIT
44

5+
use crate::AsyncInitable;
56
use crate::AsyncResult;
67
use crate::Cancellable;
78
use crate::Credentials;
@@ -31,7 +32,7 @@ use std::ptr;
3132

3233
glib::wrapper! {
3334
#[doc(alias = "GDBusConnection")]
34-
pub struct DBusConnection(Object<ffi::GDBusConnection>) @implements Initable;
35+
pub struct DBusConnection(Object<ffi::GDBusConnection>) @implements AsyncInitable, Initable;
3536

3637
match fn {
3738
type_ => || ffi::g_dbus_connection_get_type(),

gio/src/auto/dbus_proxy.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// from gir-files (https://github.com/gtk-rs/gir-files)
33
// DO NOT EDIT
44

5+
use crate::AsyncInitable;
56
use crate::AsyncResult;
67
use crate::BusType;
78
use crate::Cancellable;
@@ -29,7 +30,7 @@ use std::ptr;
2930

3031
glib::wrapper! {
3132
#[doc(alias = "GDBusProxy")]
32-
pub struct DBusProxy(Object<ffi::GDBusProxy, ffi::GDBusProxyClass>) @implements DBusInterface, Initable;
33+
pub struct DBusProxy(Object<ffi::GDBusProxy, ffi::GDBusProxyClass>) @implements AsyncInitable, DBusInterface, Initable;
3334

3435
match fn {
3536
type_ => || ffi::g_dbus_proxy_get_type(),

gio/src/auto/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ pub use self::application::Application;
2626
mod application_command_line;
2727
pub use self::application_command_line::ApplicationCommandLine;
2828

29+
mod async_initable;
30+
pub use self::async_initable::AsyncInitable;
31+
2932
mod async_result;
3033
pub use self::async_result::AsyncResult;
3134

@@ -687,6 +690,7 @@ pub mod traits {
687690
pub use super::app_launch_context::AppLaunchContextExt;
688691
pub use super::application::ApplicationExt;
689692
pub use super::application_command_line::ApplicationCommandLineExt;
693+
pub use super::async_initable::AsyncInitableExt;
690694
pub use super::async_result::AsyncResultExt;
691695
pub use super::buffered_input_stream::BufferedInputStreamExt;
692696
pub use super::buffered_output_stream::BufferedOutputStreamExt;

gio/src/auto/versions.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Generated by gir (https://github.com/gtk-rs/gir @ e0d8d8d645b1)
1+
Generated by gir (https://github.com/gtk-rs/gir @ a4ffdd5a1de1)
22
from gir-files (https://github.com/gtk-rs/gir-files @ 951202c4b7fd)

gio/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub use glib;
1414

1515
mod app_info;
1616
mod application;
17+
mod async_initable;
1718
mod cancellable;
1819
mod converter;
1920
mod data_input_stream;

0 commit comments

Comments
 (0)