Skip to content

Add --enable-method-chaining for the Rust generator.#1586

Merged
alexcrichton merged 3 commits intobytecodealliance:mainfrom
QuantumSegfault:gen-rust-method-chain
Apr 14, 2026
Merged

Add --enable-method-chaining for the Rust generator.#1586
alexcrichton merged 3 commits intobytecodealliance:mainfrom
QuantumSegfault:gen-rust-method-chain

Conversation

@QuantumSegfault
Copy link
Copy Markdown
Contributor

Resolves #1559

Implements an option for Rust guest bindings generation to have methods with no return (-> ()) instead pass-through -> &Self to allow chaining.

For consistency, this also applies to exports, so all exports normally expecting empty returns must now return self.

@QuantumSegfault
Copy link
Copy Markdown
Contributor Author

@QuantumSegfault
Copy link
Copy Markdown
Contributor Author

Thinking about this more, this feels more like a hack/workaround than an actual solution.

It works, but it's invasive (makes ALL void returns chainable; even if there's no good reason).

It's also language dependent (each language needs to do something like this themselves).

WebAssembly/component-model#334 is basically what we are trying to solve. But conversation on that stagnated.

@BjornTheProgrammer
Copy link
Copy Markdown

It is a bit of a hack due to the fact that the wit doesn't have the semantics to return the self parameter easily from a resource, but despite that I think it should still be included.

I think it is fine if this is something only used in the rust generator as well, since builder patterns like this are much more idiomatic in rust than in other languages.

Even though the function makes all void functions return references to self, I don't really believe that this creates much tangible harm. Also since it is an optional binding feature, it makes it only opt in for projects that are willing to adopt it. Eventually when wit is extended to allow some more idiomatic way of doing this without such a hack, this could always be removed.

The quality of life improvements for allowing this to be toggled in a rust project is immense. Currently a huge annoyance for people with our plugin system is that they need to write out each modification to a resource on a new line.

These are just my thoughts, and I really appreciate your great work!

Copy link
Copy Markdown
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I agree this is a workaround at best but it's also the best we have right now, and I also agree that because this is opt-in the bar isn't too high for inclusion.

Some high level comments though:

  • Can this expose the option via the generate! macro as well?
  • There's a lot of duplication of the deduction of whether to return self, could that be refactored into a helper method on the Rust type or similar? Somethign which would probably take a &Function as input for example. That helper should then be called ideally instead of duplicating the logic and/or passing around a bool

@QuantumSegfault
Copy link
Copy Markdown
Contributor Author

QuantumSegfault commented Apr 14, 2026

That should do it on the macro side?

But how do I actually run the crates/rust/tests/codegen.rs tests?

@alexcrichton
Copy link
Copy Markdown
Member

You can run the tests locally with cargo test -p wit-bindgen-rust (see CI here).

Everything here looks reasonable to me, thanks! Was there anything else you wanted to test or do before landing?

@QuantumSegfault
Copy link
Copy Markdown
Contributor Author

Ah! Got it.

Nope. This PR is ready for merge. 👍

@alexcrichton alexcrichton added this pull request to the merge queue Apr 14, 2026
Merged via the queue into bytecodealliance:main with commit be1402f Apr 14, 2026
28 checks passed
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.

Make resource function default return reference to self

3 participants