-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #602 from pbor/async-initable
gio: add AsyncInitable
- Loading branch information
Showing
21 changed files
with
240 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}), | ||
); | ||
}, | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Submodule gir
updated
26 files
+6 −6 | Cargo.lock | |
+4 −0 | book/book.toml | |
+5 −0 | book/src/config_api.md | |
+5 −5 | book/src/tutorial/high_level_rust_api.md | |
+4 −4 | book/src/tutorial/sys_library.md | |
+1 −1 | src/analysis/enums.rs | |
+1 −1 | src/analysis/flags.rs | |
+17 −5 | src/analysis/functions.rs | |
+1 −1 | src/analysis/mod.rs | |
+7 −7 | src/analysis/object.rs | |
+1 −1 | src/analysis/record.rs | |
+2 −2 | src/analysis/special_functions.rs | |
+1 −1 | src/codegen/doc/format.rs | |
+77 −23 | src/codegen/doc/gi_docgen.rs | |
+6 −0 | src/codegen/doc/mod.rs | |
+20 −0 | src/codegen/enums.rs | |
+21 −0 | src/codegen/flags.rs | |
+2 −1 | src/codegen/function.rs | |
+64 −1 | src/codegen/mod.rs | |
+11 −0 | src/codegen/object.rs | |
+2 −0 | src/codegen/signal_body.rs | |
+8 −0 | src/config/gobjects.rs | |
+1 −0 | src/library.rs | |
+1 −1 | src/library_postprocessing.rs | |
+4 −4 | src/nameutil.rs | |
+3 −0 | src/parser.rs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |