Skip to content

Commit 4f4a953

Browse files
authored
drop borrows just prior to calling task.return (#1125)
Prior to #1124, we were dropping borrows too late. That PR fixed that problem, but dropped borrows too early and didn't allow application code to capture the borrows in the `Future` it returned. This patch improves the situation by dropping borrows as late as possible, but no later. As a side effect, it simplfies the code and makes implementing the generated trait(s) much more ergonomic. Signed-off-by: Joel Dice <[email protected]>
1 parent 3b839cf commit 4f4a953

File tree

2 files changed

+14
-35
lines changed

2 files changed

+14
-35
lines changed

crates/rust/src/bindgen.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,9 @@ impl Bindgen for FunctionBindgen<'_, '_> {
932932
")".into()
933933
});
934934
}
935+
if self.async_ {
936+
self.push_str(".await");
937+
}
935938
self.push_str(";\n");
936939
}
937940

crates/rust/src/interface.rs

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl<'i> InterfaceGenerator<'i> {
189189
sig.self_arg = Some("&self".into());
190190
sig.self_is_first_param = true;
191191
}
192-
self.print_signature(func, true, &sig, false);
192+
self.print_signature(func, true, &sig);
193193
self.src.push_str(";\n");
194194
let trait_method = mem::replace(&mut self.src, prev);
195195
methods.push(trait_method);
@@ -959,7 +959,7 @@ pub mod vtable{ordinal} {{
959959
}
960960
}
961961
self.src.push_str("#[allow(unused_unsafe, clippy::all)]\n");
962-
let params = self.print_signature(func, false, &sig, true);
962+
let params = self.print_signature(func, false, &sig);
963963
self.src.push_str("{\n");
964964
self.src.push_str("unsafe {\n");
965965

@@ -1099,7 +1099,7 @@ pub mod vtable{ordinal} {{
10991099
// The corresponding close bracket is emitted in the
11001100
// `Instruction::AsyncPostCallInterface` case inside
11011101
// `FunctionBindgen::emit`.
1102-
uwriteln!(self.src, "let {name} = {{");
1102+
uwriteln!(self.src, "let {name} = async move {{");
11031103
}
11041104
for decl in handle_decls {
11051105
self.src.push_str(&decl);
@@ -1326,14 +1326,8 @@ pub mod vtable{ordinal} {{
13261326
sig.self_arg = Some("&self".into());
13271327
sig.self_is_first_param = true;
13281328
}
1329-
self.print_signature(func, true, &sig, false);
1330-
let call = if async_ {
1331-
let async_support = self.path_to_async_support();
1332-
format!("{{ #[allow(unreachable_code)]{async_support}::futures::future::ready(unreachable!()) }}\n")
1333-
} else {
1334-
"{ unreachable!() }\n".into()
1335-
};
1336-
self.src.push_str(&call);
1329+
self.print_signature(func, true, &sig);
1330+
self.src.push_str("{ unreachable!() }\n");
13371331
}
13381332

13391333
self.src.push_str("}\n");
@@ -1390,22 +1384,12 @@ pub mod vtable{ordinal} {{
13901384
// }
13911385
}
13921386

1393-
fn print_signature(
1394-
&mut self,
1395-
func: &Function,
1396-
params_owned: bool,
1397-
sig: &FnSig,
1398-
use_async_sugar: bool,
1399-
) -> Vec<String> {
1400-
let params = self.print_docs_and_params(func, params_owned, sig, use_async_sugar);
1387+
fn print_signature(&mut self, func: &Function, params_owned: bool, sig: &FnSig) -> Vec<String> {
1388+
let params = self.print_docs_and_params(func, params_owned, sig);
14011389
if let FunctionKind::Constructor(_) = &func.kind {
1402-
self.push_str(if sig.async_ && !use_async_sugar {
1403-
" -> impl ::core::future::Future<Output = Self>"
1404-
} else {
1405-
" -> Self"
1406-
})
1390+
self.push_str(" -> Self")
14071391
} else {
1408-
self.print_results(&func.results, sig.async_ && !use_async_sugar);
1392+
self.print_results(&func.results);
14091393
}
14101394
params
14111395
}
@@ -1415,7 +1399,6 @@ pub mod vtable{ordinal} {{
14151399
func: &Function,
14161400
params_owned: bool,
14171401
sig: &FnSig,
1418-
use_async_sugar: bool,
14191402
) -> Vec<String> {
14201403
self.rustdoc(&func.docs);
14211404
self.rustdoc_params(&func.params, "Parameters");
@@ -1428,7 +1411,7 @@ pub mod vtable{ordinal} {{
14281411
if sig.unsafe_ {
14291412
self.push_str("unsafe ");
14301413
}
1431-
if sig.async_ && use_async_sugar {
1414+
if sig.async_ {
14321415
self.push_str("async ");
14331416
}
14341417
self.push_str("fn ");
@@ -1518,11 +1501,8 @@ pub mod vtable{ordinal} {{
15181501
params
15191502
}
15201503

1521-
fn print_results(&mut self, results: &Results, async_: bool) {
1504+
fn print_results(&mut self, results: &Results) {
15221505
self.push_str(" -> ");
1523-
if async_ {
1524-
self.push_str("impl ::core::future::Future<Output = ");
1525-
}
15261506

15271507
match results.len() {
15281508
0 => {
@@ -1545,10 +1525,6 @@ pub mod vtable{ordinal} {{
15451525
self.push_str(")")
15461526
}
15471527
}
1548-
1549-
if async_ {
1550-
self.push_str("> + 'static");
1551-
}
15521528
}
15531529

15541530
/// Calculates the `TypeMode` to be used for the `ty` specified.

0 commit comments

Comments
 (0)