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

fix(lang/rust): link to old WIT, prose & interface use example #222

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 7 additions & 39 deletions component-model/src/language-support/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -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`

Expand Down