Skip to content

Commit

Permalink
Merge pull request #602 from pbor/async-initable
Browse files Browse the repository at this point in the history
gio: add AsyncInitable
  • Loading branch information
sdroege authored Mar 12, 2022
2 parents 6d59895 + c85972d commit bba02e3
Show file tree
Hide file tree
Showing 21 changed files with 240 additions and 16 deletions.
2 changes: 1 addition & 1 deletion gdk-pixbuf/src/auto/versions.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Generated by gir (https://github.com/gtk-rs/gir @ e0d8d8d645b1)
Generated by gir (https://github.com/gtk-rs/gir @ a4ffdd5a1de1)
from gir-files (https://github.com/gtk-rs/gir-files @ 951202c4b7fd)
2 changes: 1 addition & 1 deletion gdk-pixbuf/sys/versions.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Generated by gir (https://github.com/gtk-rs/gir @ e0d8d8d645b1)
Generated by gir (https://github.com/gtk-rs/gir @ a4ffdd5a1de1)
from gir-files (https://github.com/gtk-rs/gir-files @ 951202c4b7fd)
17 changes: 17 additions & 0 deletions gio/Gir.toml
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,23 @@ status = "generate"
name = "name"
string_type = "os_string"

[[object]]
name = "Gio.AsyncInitable"
status = "generate"
[[object.function]]
name = "init_async"
# Can be called only once
unsafe = true
[[object.function]]
name = "new_async"
manual = true
[[object.function]]
name = "new_valist_async"
ignore = true
[[object.function]]
name = "newv_async"
ignore = true

[[object]]
name = "Gio.BufferedInputStream"
status = "generate"
Expand Down
93 changes: 93 additions & 0 deletions gio/src/async_initable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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));
}),
);
},
))
}
}
107 changes: 107 additions & 0 deletions gio/src/auto/async_initable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::AsyncResult;
use crate::Cancellable;
use glib::object::IsA;
use glib::translate::*;
use std::boxed::Box as Box_;
use std::fmt;
use std::pin::Pin;
use std::ptr;

glib::wrapper! {
#[doc(alias = "GAsyncInitable")]
pub struct AsyncInitable(Interface<ffi::GAsyncInitable, ffi::GAsyncInitableIface>);

match fn {
type_ => || ffi::g_async_initable_get_type(),
}
}

impl AsyncInitable {
pub const NONE: Option<&'static AsyncInitable> = None;
}

pub trait AsyncInitableExt: 'static {
#[doc(alias = "g_async_initable_init_async")]
unsafe fn init_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
&self,
io_priority: glib::Priority,
cancellable: Option<&impl IsA<Cancellable>>,
callback: P,
);

unsafe fn init_future(
&self,
io_priority: glib::Priority,
) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>>;
}

impl<O: IsA<AsyncInitable>> AsyncInitableExt for O {
unsafe fn init_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
&self,
io_priority: glib::Priority,
cancellable: Option<&impl IsA<Cancellable>>,
callback: P,
) {
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);

let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn init_async_trampoline<P: FnOnce(Result<(), glib::Error>) + 'static>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut crate::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
let mut error = ptr::null_mut();
let _ = ffi::g_async_initable_init_finish(_source_object as *mut _, res, &mut error);
let result = if error.is_null() {
Ok(())
} else {
Err(from_glib_full(error))
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback: P = callback.into_inner();
callback(result);
}
let callback = init_async_trampoline::<P>;
ffi::g_async_initable_init_async(
self.as_ref().to_glib_none().0,
io_priority.into_glib(),
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}

unsafe fn init_future(
&self,
io_priority: glib::Priority,
) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
Box_::pin(crate::GioFuture::new(
self,
move |obj, cancellable, send| {
obj.init_async(io_priority, Some(cancellable), move |res| {
send.resolve(res);
});
},
))
}
}

impl fmt::Display for AsyncInitable {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("AsyncInitable")
}
}
3 changes: 2 additions & 1 deletion gio/src/auto/dbus_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::AsyncInitable;
use crate::AsyncResult;
use crate::Cancellable;
use crate::Credentials;
Expand Down Expand Up @@ -31,7 +32,7 @@ use std::ptr;

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

match fn {
type_ => || ffi::g_dbus_connection_get_type(),
Expand Down
3 changes: 2 additions & 1 deletion gio/src/auto/dbus_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::AsyncInitable;
use crate::AsyncResult;
use crate::BusType;
use crate::Cancellable;
Expand Down Expand Up @@ -29,7 +30,7 @@ use std::ptr;

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

match fn {
type_ => || ffi::g_dbus_proxy_get_type(),
Expand Down
4 changes: 4 additions & 0 deletions gio/src/auto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ pub use self::application::Application;
mod application_command_line;
pub use self::application_command_line::ApplicationCommandLine;

mod async_initable;
pub use self::async_initable::AsyncInitable;

mod async_result;
pub use self::async_result::AsyncResult;

Expand Down Expand Up @@ -687,6 +690,7 @@ pub mod traits {
pub use super::app_launch_context::AppLaunchContextExt;
pub use super::application::ApplicationExt;
pub use super::application_command_line::ApplicationCommandLineExt;
pub use super::async_initable::AsyncInitableExt;
pub use super::async_result::AsyncResultExt;
pub use super::buffered_input_stream::BufferedInputStreamExt;
pub use super::buffered_output_stream::BufferedOutputStreamExt;
Expand Down
2 changes: 1 addition & 1 deletion gio/src/auto/versions.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Generated by gir (https://github.com/gtk-rs/gir @ e0d8d8d645b1)
Generated by gir (https://github.com/gtk-rs/gir @ a4ffdd5a1de1)
from gir-files (https://github.com/gtk-rs/gir-files @ 951202c4b7fd)
1 change: 1 addition & 0 deletions gio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub use glib;

mod app_info;
mod application;
mod async_initable;
mod cancellable;
mod converter;
mod data_input_stream;
Expand Down
2 changes: 1 addition & 1 deletion gio/sys/versions.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Generated by gir (https://github.com/gtk-rs/gir @ e0d8d8d645b1)
Generated by gir (https://github.com/gtk-rs/gir @ a4ffdd5a1de1)
from gir-files (https://github.com/gtk-rs/gir-files @ 951202c4b7fd)
2 changes: 1 addition & 1 deletion glib/gobject-sys/versions.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Generated by gir (https://github.com/gtk-rs/gir @ e0d8d8d645b1)
Generated by gir (https://github.com/gtk-rs/gir @ a4ffdd5a1de1)
from gir-files (https://github.com/gtk-rs/gir-files @ 951202c4b7fd)
2 changes: 1 addition & 1 deletion glib/src/auto/versions.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Generated by gir (https://github.com/gtk-rs/gir @ e0d8d8d645b1)
Generated by gir (https://github.com/gtk-rs/gir @ a4ffdd5a1de1)
from gir-files (https://github.com/gtk-rs/gir-files @ 951202c4b7fd)
2 changes: 1 addition & 1 deletion glib/sys/versions.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Generated by gir (https://github.com/gtk-rs/gir @ e0d8d8d645b1)
Generated by gir (https://github.com/gtk-rs/gir @ a4ffdd5a1de1)
from gir-files (https://github.com/gtk-rs/gir-files @ 951202c4b7fd)
2 changes: 1 addition & 1 deletion graphene/src/auto/versions.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Generated by gir (https://github.com/gtk-rs/gir @ e0d8d8d645b1)
Generated by gir (https://github.com/gtk-rs/gir @ a4ffdd5a1de1)
from gir-files (https://github.com/gtk-rs/gir-files @ 951202c4b7fd)
2 changes: 1 addition & 1 deletion graphene/sys/versions.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Generated by gir (https://github.com/gtk-rs/gir @ e0d8d8d645b1)
Generated by gir (https://github.com/gtk-rs/gir @ a4ffdd5a1de1)
from gir-files (https://github.com/gtk-rs/gir-files @ 951202c4b7fd)
2 changes: 1 addition & 1 deletion pango/src/auto/versions.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Generated by gir (https://github.com/gtk-rs/gir @ e0d8d8d645b1)
Generated by gir (https://github.com/gtk-rs/gir @ a4ffdd5a1de1)
from gir-files (https://github.com/gtk-rs/gir-files @ 951202c4b7fd)
2 changes: 1 addition & 1 deletion pango/sys/versions.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Generated by gir (https://github.com/gtk-rs/gir @ e0d8d8d645b1)
Generated by gir (https://github.com/gtk-rs/gir @ a4ffdd5a1de1)
from gir-files (https://github.com/gtk-rs/gir-files @ 951202c4b7fd)
2 changes: 1 addition & 1 deletion pangocairo/src/auto/versions.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Generated by gir (https://github.com/gtk-rs/gir @ e0d8d8d645b1)
Generated by gir (https://github.com/gtk-rs/gir @ a4ffdd5a1de1)
from gir-files (https://github.com/gtk-rs/gir-files @ 951202c4b7fd)
2 changes: 1 addition & 1 deletion pangocairo/sys/versions.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Generated by gir (https://github.com/gtk-rs/gir @ e0d8d8d645b1)
Generated by gir (https://github.com/gtk-rs/gir @ a4ffdd5a1de1)
from gir-files (https://github.com/gtk-rs/gir-files @ 951202c4b7fd)

0 comments on commit bba02e3

Please sign in to comment.