From bfbec8a6592784c496947dd45664d7de0aae4f63 Mon Sep 17 00:00:00 2001 From: Kinflou Date: Sun, 15 Nov 2026 22:52:34 +0800 Subject: [PATCH] Shorten the async trait signatures with a Future import An async connection protocol's handlers were written `-> impl ::core::future::Future + Send`. They now import `Future` and write `-> impl Future + Send`; the `+ Send` stays, since the dispatcher boxes the handlers' futures into a `BoxFuture` and an `async fn` in a trait can't state that bound. `Future` goes through the same marks as the other names generated code uses: the import is emitted only where a signature names it, and a module that declares its own `Future` keeps the full path instead. --- codegen/src/generator.rs | 3 ++- codegen/src/names.rs | 1 + codegen/tests/compiles.rs | 2 ++ codegen/tests/generate.rs | 55 ++++++++++++++++++++++++++++++++++++--- 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/codegen/src/generator.rs b/codegen/src/generator.rs index 711a859..960f3c7 100644 --- a/codegen/src/generator.rs +++ b/codegen/src/generator.rs @@ -727,6 +727,7 @@ fn schema_source( // The framings, format and handshake a body names by mark and no import // above covers. let extra: String = [ + ("::core::future", "Future"), ("comline_runtime::contract", "Datagram3Framing"), ("comline_runtime::format", "Json"), ("comline_runtime::framing", "JsonRpcFraming"), @@ -2418,7 +2419,7 @@ fn protocol( format!("$Result<{}, $Fault<{}>>", f.ret, f.err_ty) }; s.push_str(&format!( - " fn {}(&self{args}) -> impl ::core::future::Future + Send;\n", + " fn {}(&self{args}) -> impl $Future + Send;\n", f.ident )); } else if f.one_way { diff --git a/codegen/src/names.rs b/codegen/src/names.rs index 2cff0de..d6577c3 100644 --- a/codegen/src/names.rs +++ b/codegen/src/names.rs @@ -25,6 +25,7 @@ const PATHS: &[(&str, &str)] = &[ ("Vec", "::std::vec::Vec"), ("String", "::std::string::String"), ("Box", "::std::boxed::Box"), + ("Future", "::core::future::Future"), ("Serialize", "::serde::Serialize"), ("Deserialize", "::serde::Deserialize"), ("Client", "::comline_runtime::client::Client"), diff --git a/codegen/tests/compiles.rs b/codegen/tests/compiles.rs index 39ef1d2..ed9fad5 100644 --- a/codegen/tests/compiles.rs +++ b/codegen/tests/compiles.rs @@ -1924,6 +1924,7 @@ fn colliding_schemas() -> Vec<(String, Vec)> { "Vec", "Result", "Box", + "Future", "Serialize", "Deserialize", "Client", @@ -2699,6 +2700,7 @@ fn protocols_named_like_the_package_modules_types_build() { "Comline2Handshake", "Unavailable", "Result", + "Future", "Services", ]; let shared = std::env::temp_dir().join(format!("comline-rust-names-{}", std::process::id())); diff --git a/codegen/tests/generate.rs b/codegen/tests/generate.rs index e005d02..5032063 100644 --- a/codegen/tests/generate.rs +++ b/codegen/tests/generate.rs @@ -1982,10 +1982,9 @@ fn async_makes_a_connection_protocol_async() { src.contains("pub trait Rpc: Send + Sync + 'static {"), "{src}" ); - assert!( - src.contains("-> impl ::core::future::Future impl Future AsyncDispatch for RpcAsyncDispatcher {"), @@ -2030,6 +2029,54 @@ fn async_makes_a_connection_protocol_async() { assert!(!src.contains("Async"), "{src}"); } +/// The async handlers' `impl Future` is written by its imported name, which +/// is only imported where it is used; a schema that declares a `Future` of its +/// own keeps the full path and imports nothing. +#[test] +fn the_future_import_is_only_where_it_is_used_and_free() { + let one = |async_api: bool, declare: bool| { + let mut units = one_plain_protocol("Rpc"); + if declare { + units.push(FrozenUnit::Struct { + docstring: None, + parameters: vec![], + name: "Future".into(), + fields: vec![], + span: (0, 0), + }); + } + let schemas = vec![("rpc".to_string(), units)]; + let req = GenerateRequest { + async_api, + ..code_req(&schemas) + }; + generate_rust(&req).unwrap().remove(0).contents + }; + + let blocking = one(false, false); + assert!(!blocking.contains("Future"), "{blocking}"); + + let asynchronous = one(true, false); + assert_eq!( + asynchronous.matches("use ::core::future::Future;").count(), + 1 + ); + assert!( + asynchronous.contains("-> impl Future impl ::core::future::Future