Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change preview2 builder methods to use &mut self #6770

Merged
merged 6 commits into from
Aug 5, 2023

Conversation

alexcrichton
Copy link
Member

This commit changes the WasiCtxBuilder for preview2 to use a builder pattern more similar to std::process::Command where methods take &mut self and return &mut Self instead of taking self and returning Self. This pattern enables more easily building up configuration over time throughout code where ownership transfer might otherwise be awkward.

A small caveat to this is that the ergonomics of this pattern only really work out well if the final "build" method takes &mut self as well. In this situation it's difficult to try to figure out what's supposed to happen if this method is called twice, so I left it to panic for now so we can more easily update it in the future possibly.

I'll also note that the original motivations for this come from the upgrade at fermyon/spin#1678

@alexcrichton alexcrichton requested a review from a team as a code owner July 25, 2023 14:36
@alexcrichton alexcrichton requested review from jameysharp and removed request for a team July 25, 2023 14:36
@pchickey
Copy link
Contributor

I used the same builder design pattern as the previous generation: wasi-cap-std-sync and wasi-tokio WasiCtxBuilder. The idea was that this would make it easiest for folks to upgrade. From the diff here https://github.com/fermyon/spin/pull/1678/files I'm not sure what @tschneidereit found annoying - the changes to preview2::WasiCtxBuilder across those versions was just a change to the clock setters, and in his diff he eliminated use of mem::swap and it appears to be uniform with the preview1 implementation.

@jameysharp
Copy link
Contributor

@pchickey, do you want to take the review on this PR?

@pchickey pchickey requested review from pchickey and removed request for jameysharp July 25, 2023 15:58
@alexcrichton
Copy link
Member Author

Ah thanks for checking @pchickey, it looks like the issue is:

enum WasiCtxBuilder {
    Preview1(wasi_preview1::WasiCtx),
    Preview2(wasi_preview2::WasiCtxBuilder),
}

where one's using a builder and one isn't. If both used a builder then it'd have the same pattern, which I believe would resolve the original motivation for this PR.

That being said though I personally prefer &mut self-style builders anyway, so if you agree that it'd be ok to move I'll update the other builders I missed here in the cap-std-sync and tokio crates?

@pchickey
Copy link
Contributor

Understood. Yes, we got rid of all the public &mut self methods on preview 2 WasiCtx that could substitute for the builder, mostly because of interactions with the table. So, now the WasiCtxBuilder is the only way to set things up.

We can switch all the builders to &mut self if you think that is better ergonomics. I dont particularly love that .build() is &mut self, because unlike in the Command case the builder has taken ownership of various resources and I liked to see .build() transferring that ownership to the WasiCtx (in addition to making it impossible to build it twice). But at the end of the day its just syntax, so whatever.

@github-actions github-actions bot added the wasi Issues pertaining to WASI label Jul 25, 2023
@alexcrichton
Copy link
Member Author

Ok let's stick with the move-style builder and see how far it gets us, can always revisit later!

@tschneidereit
Copy link
Member

I'm not sure what @tschneidereit found annoying - the changes to preview2::WasiCtxBuilder across those versions was just a change to the clock setters, and in his diff he eliminated use of mem::swap and it appears to be uniform with the preview1 implementation.

The annoying part is having to extract the dummy ctx builder instance, and then use one more lambda to use it. It's not the end of the world, but it is somewhat unwieldy. And I think it wouldn't become too much simpler even if preview1 weren't in the mix, either.

My main concern isn't so much our use in Spin specifically, but more generally that this seems plausible to be hit by embedders more generally.

@pchickey
Copy link
Contributor

Ok. I'm fine with changing it to a &mut Self style builder. It would be nice to keep preview 1 consistent but also its not that important.

@alexcrichton alexcrichton restored the mut-builder branch July 26, 2023 15:57
@alexcrichton alexcrichton reopened this Jul 26, 2023
@alexcrichton
Copy link
Member Author

Ok I'll reopen but will work on getting the preview1 bits updated.

@alexcrichton
Copy link
Member Author

Ok I've additionally renamed methods on preview2 builders to match preview1 builder (e.g. args instead of set_args). I've additionally updated the semantics slightly where now args will append all its arguments instead of overwriting to match the behavior of preview1 and other builders like Command

@pchickey pchickey added this pull request to the merge queue Jul 28, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jul 28, 2023
@github-actions github-actions bot added the wasmtime:c-api Issues pertaining to the C API. label Aug 2, 2023
@github-actions
Copy link

github-actions bot commented Aug 2, 2023

Subscribe to Label Action

cc @peterhuene

This issue or pull request has been labeled: "wasi", "wasmtime:c-api"

Thus the following users have been cc'd because of the following labels:

  • peterhuene: wasmtime:c-api

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

This commit changes the `WasiCtxBuilder` for preview2 to use a builder
pattern more similar to `std::process::Command` where methods take `&mut
self` and return `&mut Self` instead of taking `self` and returning
`Self`. This pattern enables more easily building up configuration over
time throughout code where ownership transfer might otherwise be
awkward.

A small caveat to this is that the ergonomics of this pattern only
really work out well if the final "build" method takes `&mut self` as
well. In this situation it's difficult to try to figure out what's
supposed to happen if this method is called twice, so I left it to panic
for now so we can more easily update it in the future possibly.
* Move preview1 builders to `&mut`-style
* Rename methods on preview2 builder to match names on the preview1 builders
@alexcrichton alexcrichton added this pull request to the merge queue Aug 5, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 5, 2023
@alexcrichton alexcrichton requested a review from a team as a code owner August 5, 2023 15:30
@alexcrichton alexcrichton added this pull request to the merge queue Aug 5, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 5, 2023
@alexcrichton alexcrichton added this pull request to the merge queue Aug 5, 2023
Merged via the queue into bytecodealliance:main with commit bb3734b Aug 5, 2023
19 checks passed
@alexcrichton alexcrichton deleted the mut-builder branch August 5, 2023 17:41
geekbeast pushed a commit to geekbeast/wasmtime that referenced this pull request Aug 6, 2023
… feature/preview2

* 'feature/preview2' of github.com:geekbeast/wasmtime:
  Change preview2 builder methods to use `&mut self` (bytecodealliance#6770)
  Add a bindgen test that exercises using error types from a different interface (bytecodealliance#6802)
  Resolve trappable error types with fully qualified package paths (bytecodealliance#6795)
  Update the dev-dependency for wit-bindgen to 0.9.0 (bytecodealliance#6800)
  Fix incorrect sample code in documentation (bytecodealliance#6796) (bytecodealliance#6797)
  Update preview1 to trap on misaligned pointers (bytecodealliance#6776)
  Fix posix-signals-on-macos on aarch64-apple-darwin (bytecodealliance#6793)
  consistient WASI preview1 rights reporting (bytecodealliance#6784)
  Wasmtime: Introduce `{Module,Component}::resources_required` (bytecodealliance#6789)
eduardomourar pushed a commit to eduardomourar/wasmtime that referenced this pull request Aug 18, 2023
…6770)

* Change preview2 builder methods to use `&mut self`

This commit changes the `WasiCtxBuilder` for preview2 to use a builder
pattern more similar to `std::process::Command` where methods take `&mut
self` and return `&mut Self` instead of taking `self` and returning
`Self`. This pattern enables more easily building up configuration over
time throughout code where ownership transfer might otherwise be
awkward.

A small caveat to this is that the ergonomics of this pattern only
really work out well if the final "build" method takes `&mut self` as
well. In this situation it's difficult to try to figure out what's
supposed to happen if this method is called twice, so I left it to panic
for now so we can more easily update it in the future possibly.

* Synchronize preview1/preview2 builders

* Move preview1 builders to `&mut`-style
* Rename methods on preview2 builder to match names on the preview1 builders

* Fix C API

* Fix more tests

* Fix benchmark build

* Fix unused variable
saulecabrera added a commit to bytecodealliance/wasmtime-rb that referenced this pull request Sep 26, 2023
* Updates to `wasmtime` 13.0.0
* Update the usage of the `WasiCtxBuilder` which as of this update its
  methods return `&mut Self` bytecodealliance/wasmtime#6770
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasi Issues pertaining to WASI wasmtime:c-api Issues pertaining to the C API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants