Skip to content

codegen: shorter async trait signatures (import Future) - #134

Draft
Kinflou wants to merge 1 commit into
mainfrom
codegen/shorter-async-signatures
Draft

Kinflou wants to merge 1 commit into
mainfrom
codegen/shorter-async-signatures

Conversation

@Kinflou

@Kinflou Kinflou commented Nov 15, 2026

Copy link
Copy Markdown
Member

Under async = true, a connection protocol's handlers are generated as

fn send(&self, peer: &impl AsyncPeer, n: u32) -> impl ::core::future::Future<Output = Result<u32, Fault<ChatSendError>>> + Send;

They are now

use ::core::future::Future;
...
fn send(&self, peer: &impl AsyncPeer, n: u32) -> impl Future<Output = Result<u32, Fault<ChatSendError>>> + Send;

The + Send stays: the async dispatcher boxes handler futures into the runtime's BoxFuture, and an async fn in a trait can't state that bound on the 1.85 floor. Only the path is shortened.

How

Future is added to the generator's marked names (names.rs), the same mechanism as Peer, Fault, Result. The template writes $Future; the import is emitted at the top of the file only when the body marks it, so sync output is unchanged and an async file with no connection protocol gets no import. impl Future<...> + Send in the trait is the only place the generator emits such a signature (the sender and dispatcher are async fn), so there is one use site.

A schema type named Future

Handled like a schema type named Peer or Fault (comline-rust#96): the module's declared names (own types, protocols, derived names) are checked, and where Future is one of them the import is left out and the signature keeps the full ::core::future::Future path. It is not aliased. That is a per-module decision, so a file without a Future of its own gets the short form even if another file in the package declares one. Future is not added to the trait-in-scope list: the signature only names it, no method of it is called.

Tests:

  • generate.rs: the async output has the import once and the short signature; sync output contains no Future; a declared Future keeps the full path and no import.
  • compiles.rs: Future joins the colliding names (colliding_schemas, built sync and async) and the protocol names that build, so both a struct and a protocol named Future are compiled, async included.

Generation goldens that will change

After this merges, the one golden in ComlineProject/generation with ::core::future::Future needs re-blessing (not part of this PR):

  • conformance/tests/golden/connections_async/rust/conformance.rs: lines 29, 122 and 123 shorten, and use ::core::future::Future; is added to its imports.

connections_async/rust/connection.rs and the other goldens don't contain it. std-extra/ has no async signatures, so it is unaffected and STD_REV is not touched.

An async connection protocol's handlers were written
`-> impl ::core::future::Future<Output = ..> + Send`. They now import
`Future` and write `-> impl Future<Output = ..> + 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant