-
Notifications
You must be signed in to change notification settings - Fork 66
fix(lang/rust): link to old WIT, prose & interface use example #222
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
Merged
kate-goldenring
merged 5 commits into
bytecodealliance:main
from
vados-cosmonic:fix(rust)=wit-link-and-prose-for-interfaces-with-example
Apr 7, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eb15c5e
fix(lang/rust): link to old WIT, prose & interface use example
vados-cosmonic ec6b327
fix(lang/rust): prose
vados-cosmonic 266e9f3
fix(lang/rust): prose
vados-cosmonic 803be65
fix(lang/rust): remove more advanced interface example
vados-cosmonic 534c3ea
fix(lang/rust): naming of adder.wasm
vados-cosmonic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,19 +17,19 @@ cargo install cargo-component | |
|
||
## 2. Scaffold a Component with `cargo component` | ||
|
||
Create a Rust library that implements the `add` function in the [`adder`world][adder-wit]. | ||
Create a Rust library that implements the `add` function in the [`adder` world][adder-wit]. | ||
|
||
First scaffold a project: | ||
|
||
```sh | ||
$ cargo component new add --lib && cd add | ||
$ cargo component new adder --lib && cd adder | ||
``` | ||
|
||
Note that `cargo component` generates the necessary bindings as a module called `bindings`. | ||
|
||
## 3. Add the WIT world the Component will implement | ||
|
||
Next, update `wit/world.wit` to match `add.wit`: | ||
Next, update `wit/world.wit` to match the [`adder` world][adder-wit]: | ||
|
||
``` | ||
package docs:[email protected]; | ||
|
@@ -95,7 +95,7 @@ cargo component build --release | |
You can use `wasm-tools component wit` to output the WIT package of the component: | ||
|
||
``` | ||
$ wasm-tools component wit target/wasm32-wasip1/release/add.wasm | ||
$ wasm-tools component wit target/wasm32-wasip1/release/adder.wasm | ||
package root:component; | ||
|
||
world root { | ||
|
@@ -118,43 +118,10 @@ Rust bindings, bring in WASI worlds, and execute the component. | |
|
||
```sh | ||
$ cd examples/example-host | ||
$ cargo run --release -- 1 2 ../add/target/wasm32-wasip1/release/add.wasm | ||
$ cargo run --release -- 1 2 ../add/target/wasm32-wasip1/release/adder.wasm | ||
1 + 2 = 3 | ||
``` | ||
|
||
## Exporting an interface with `cargo component` | ||
|
||
The [sample `add.wit` file](https://github.com/bytecodealliance/component-docs/tree/main/component-model/examples/example-host/add.wit) exports a function. However, you'll often prefer to export an interface, either to comply with an existing specification or to capture a set of functions and types that tend to go together. For example, to implement the following world: | ||
|
||
```wit | ||
package docs:[email protected]; | ||
|
||
interface add { | ||
add: func(x: u32, y: u32) -> u32; | ||
} | ||
|
||
world adder { | ||
export add; | ||
} | ||
``` | ||
|
||
you would write the following Rust code: | ||
|
||
```rust | ||
mod bindings; | ||
|
||
// Separating out the interface puts it in a sub-module | ||
use bindings::exports::docs::adder::add::Guest; | ||
|
||
struct Component; | ||
|
||
impl Guest for Component { | ||
fn add(x: u32, y: u32) -> u32 { | ||
x + y | ||
} | ||
} | ||
``` | ||
|
||
## Importing an interface with `cargo component` | ||
|
||
The world file (`wit/world.wit`) generated for you by `cargo component new --lib` doesn't specify any imports. | ||
|
@@ -234,7 +201,8 @@ world root { | |
} | ||
``` | ||
|
||
As the import is unfulfilled, the `calculator.wasm` component could not run by itself in its current form. To fulfill the `add` import, so that only `calculate` is exported, you would need to [compose the `calculator.wasm` with some `exports-add.wasm` into a single, self-contained component](../creating-and-consuming/composing.md). | ||
As the import is unfulfilled, the `calculator.wasm` component could not run by itself in its current form. To fulfill the `add` import, so that | ||
only `calculate` is exported, you would need to [compose the `calculator.wasm` with some `adder.wasm` into a single, self-contained component](../creating-and-consuming/composing.md). | ||
|
||
## Creating a command component with `cargo component` | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.