Skip to content

Commit

Permalink
Merge pull request #1315 from sdroege/0.15-backports
Browse files Browse the repository at this point in the history
[0.15] Don't require `Send` closures for GIO-style async functions
  • Loading branch information
sdroege committed Jan 28, 2022
2 parents ee37253 + e181b33 commit 78e3d3c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
6 changes: 2 additions & 4 deletions src/analysis/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,8 @@ impl Bounds {
}
let parameters = format_out_parameters(&out_parameters);
let error_type = find_error_type(env, function);
type_string = format!(
"FnOnce(Result<{}, {}>) + Send + 'static",
parameters, error_type
);
type_string =
format!("FnOnce(Result<{}, {}>) + 'static", parameters, error_type);
let bound_name = *self.unused.front().unwrap();
callback_info = Some(CallbackInfo {
callback_type: type_string.clone(),
Expand Down
34 changes: 31 additions & 3 deletions src/codegen/function_body_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,12 +716,30 @@ impl Builder {
chunks: &mut Vec<Chunk>,
trampoline: &AsyncTrampoline,
) {
chunks.push(Chunk::Custom(String::from(
r###"
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"
);
"###,
)));

chunks.push(Chunk::Let {
name: "user_data".to_string(),
is_mut: false,
value: Box::new(Chunk::Custom("Box_::new(callback)".into())),
value: Box::new(Chunk::Custom(format!(
"Box_::new({}::new(callback))",
use_glib_type(env, "thread_guard::ThreadGuard")
))),
type_: Some(Box::new(Chunk::Custom(format!(
"Box_<{}>",
"Box_<{}<{}>>",
use_glib_type(env, "thread_guard::ThreadGuard"),
trampoline.bound_name
)))),
});
Expand Down Expand Up @@ -874,7 +892,17 @@ impl Builder {
is_mut: false,
value: Box::new(Chunk::Custom("Box_::from_raw(user_data as *mut _)".into())),
type_: Some(Box::new(Chunk::Custom(format!(
"Box_<{}>",
"Box_<{}<{}>>",
use_glib_type(env, "thread_guard::ThreadGuard"),
trampoline.bound_name
)))),
});
body.push(Chunk::Let {
name: "callback".to_string(),
is_mut: false,
value: Box::new(Chunk::Custom("callback.into_inner()".into())),
type_: Some(Box::new(Chunk::Custom(format!(
"{}",
trampoline.bound_name
)))),
});
Expand Down
4 changes: 1 addition & 3 deletions src/codegen/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ fn function_type_string(
analysis: &analysis::signals::Info,
closure: bool,
) -> Option<String> {
if analysis.trampoline.is_err() {
return None;
}
analysis.trampoline.as_ref().ok()?;

let trampoline = analysis.trampoline.as_ref().unwrap_or_else(|_| {
panic!(
Expand Down

0 comments on commit 78e3d3c

Please sign in to comment.