Skip to content

Commit

Permalink
fix(rs-bindgen): workaround the async Send compiler bug
Browse files Browse the repository at this point in the history
fix rust-lang/rust#96865 in bindgen

Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net>
  • Loading branch information
rvolosatovs committed Jul 5, 2024
1 parent 38be7d1 commit c8b4006
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 27 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ wasmtime = { version = "22", default-features = false }
wasmtime-wasi = { version = "22", default-features = false }
wit-bindgen = { version = "0.27", default-features = false }
wit-bindgen-core = { version = "0.27", default-features = false }
wit-bindgen-wrpc = { version = "0.4.2", default-features = false, path = "./crates/wit-bindgen" }
wit-bindgen-wrpc = { version = "0.4.3", default-features = false, path = "./crates/wit-bindgen" }
wit-bindgen-wrpc-go = { version = "0.2", default-features = false, path = "./crates/wit-bindgen-go" }
wit-bindgen-wrpc-rust = { version = "0.4.2", default-features = false, path = "./crates/wit-bindgen-rust" }
wit-bindgen-wrpc-rust-macro = { version = "0.4.2", default-features = false, path = "./crates/wit-bindgen-rust-macro" }
wit-bindgen-wrpc-rust = { version = "0.4.3", default-features = false, path = "./crates/wit-bindgen-rust" }
wit-bindgen-wrpc-rust-macro = { version = "0.4.3", default-features = false, path = "./crates/wit-bindgen-rust-macro" }
wit-component = { version = "0.212", default-features = false }
wit-parser = { version = "0.212", default-features = false }
wrpc-cli = { version = "0.2", path = "./crates/cli", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion crates/wit-bindgen-rust-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wit-bindgen-wrpc-rust-macro"
version = "0.4.2"
version = "0.4.3"
description = """
Procedural macro paired with the `wit-bindgen-wrpc` crate.
"""
Expand Down
2 changes: 1 addition & 1 deletion crates/wit-bindgen-rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wit-bindgen-wrpc-rust"
version = "0.4.2"
version = "0.4.3"
description = """
Rust bindings generator for wRPC, typically used through
the `wit-bindgen-wrpc` crate's `generate!` macro.
Expand Down
55 changes: 37 additions & 18 deletions crates/wit-bindgen-rust/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,10 +536,16 @@ pub async fn serve_interface<T: {wrpc_transport}::Serve, U>(
let params = self.print_docs_and_params(func, &sig);
match func.results.iter_types().collect::<Vec<_>>().as_slice() {
[] => {
uwrite!(self.src, " -> {anyhow}::Result<()>",);
uwrite!(
self.src,
" -> impl ::core::future::Future<Output = {anyhow}::Result<()>> + Send + 'a"
);
}
[ty] => {
uwrite!(self.src, " -> {anyhow}::Result<",);
uwrite!(
self.src,
" -> impl ::core::future::Future<Output = {anyhow}::Result<"
);
if async_params || !paths.is_empty() {
self.push_str("(");
}
Expand All @@ -551,10 +557,13 @@ pub async fn serve_interface<T: {wrpc_transport}::Serve, U>(
wrpc_transport = self.gen.wrpc_transport_path(),
);
}
uwrite!(self.src, ">");
uwrite!(self.src, ">> + Send + 'a");
}
types => {
uwrite!(self.src, " -> {anyhow}::Result<(",);
uwrite!(
self.src,
" -> impl ::core::future::Future<Output = {anyhow}::Result<(",
);
for ty in types {
self.print_ty(ty, true, false);
self.push_str(", ");
Expand All @@ -566,17 +575,23 @@ pub async fn serve_interface<T: {wrpc_transport}::Serve, U>(
wrpc_transport = self.gen.wrpc_transport_path(),
);
}
uwrite!(self.src, ")>");
uwrite!(self.src, ")>> + Send + 'a");
}
};
self.src.push_str("{\n");
self.push_str(
r"
{
async move {",
);

if func.results.len() == 0 || (!async_params && paths.is_empty()) {
uwrite!(
self.src,
r#"let wrpc__ = {anyhow}::Context::context(
wrpc__.invoke_values_blocking(cx__, "{instance}", "{}", ({params}), "#,
r#"
let wrpc__ = {anyhow}::Context::context(
{wrpc_transport}::SendFuture::send(wrpc__.invoke_values_blocking(cx__, "{instance}", "{}", ({params}), "#,
rpc_func_name(func),
wrpc_transport = self.gen.wrpc_transport_path(),
params = {
let s = params.join(", ");
if params.len() == 1 {
Expand All @@ -595,11 +610,11 @@ pub async fn serve_interface<T: {wrpc_transport}::Serve, U>(
self.src.push_str(".as_slice(), ");
}
}
self.src.push_str("]).await,\n");
self.src.push_str("])).await,\n");
uwriteln!(
self.src,
r#"
"failed to invoke `{instance}.{}`")?;"#,
"failed to invoke `{instance}.{}`")?;"#,
func.name,
);
if func.results.len() == 1 {
Expand All @@ -609,9 +624,11 @@ pub async fn serve_interface<T: {wrpc_transport}::Serve, U>(
} else {
uwrite!(
self.src,
r#"let (wrpc__, io__) = {anyhow}::Context::context(
wrpc__.invoke_values(cx__, "{instance}", "{}", ({params}), "#,
r#"
let (wrpc__, io__) = {anyhow}::Context::context(
{wrpc_transport}::SendFuture::send(wrpc__.invoke_values(cx__, "{instance}", "{}", ({params}), "#,
rpc_func_name(func),
wrpc_transport = self.gen.wrpc_transport_path(),
params = {
let s = params.join(", ");
if params.len() == 1 {
Expand All @@ -630,11 +647,11 @@ pub async fn serve_interface<T: {wrpc_transport}::Serve, U>(
self.src.push_str(".as_slice(), ");
}
}
self.src.push_str("]).await,\n");
self.src.push_str("])).await,\n");
uwriteln!(
self.src,
r#"
"failed to invoke `{instance}.{}`")?;"#,
"failed to invoke `{instance}.{}`")?;"#,
func.name,
);
if func.results.len() == 1 {
Expand All @@ -653,7 +670,12 @@ pub async fn serve_interface<T: {wrpc_transport}::Serve, U>(
self.push_str("io__))\n");
}
}
self.push_str("}\n");
uwriteln!(
self.src,
r"
}}
}}"
);

match func.kind {
FunctionKind::Freestanding => {}
Expand Down Expand Up @@ -723,9 +745,6 @@ pub async fn serve_interface<T: {wrpc_transport}::Serve, U>(
if !sig.private {
self.push_str("pub ");
}
if self.in_import {
self.push_str("async ");
}
self.push_str("fn ");
if sig.use_item_name {
if let FunctionKind::Constructor(_) = &func.kind {
Expand Down
2 changes: 1 addition & 1 deletion crates/wit-bindgen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wit-bindgen-wrpc"
version = "0.4.2"
version = "0.4.3"
description = """
Rust bindings generator for wRPC.
"""
Expand Down

0 comments on commit c8b4006

Please sign in to comment.