Skip to content

Commit f59c26e

Browse files
refactor(pl/rust): clarify use of bindings
1 parent 4599536 commit f59c26e

File tree

1 file changed

+25
-12
lines changed
  • component-model/src/language-support/importing-and-reusing-components

1 file changed

+25
-12
lines changed

component-model/src/language-support/importing-and-reusing-components/rust.md

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
The world file (`wit/world.wit`) we generated doesn't specify any imports.
66
If your component consumes other components, you can edit the `world.wit` file to import their interfaces.
77

8-
For example, suppose you have created and built an adder component as explained in the [exporting an interface section](#exporting-an-interface-with-cargo-component) and want to use that component in a calculator component. Here is a partial example world for a calculator that imports the add interface:
8+
For example, suppose you have created and built the adder component as explained in the earlier tutorials and want to use
9+
that component in a calculator component. Here is a partial example world for a calculator that imports the add interface:
910

1011
```wit
1112
// in the 'calculator' project
@@ -18,8 +19,9 @@ interface calculate {
1819
}
1920
2021
world calculator {
21-
export calculate;
2222
import docs:adder/add@0.1.0;
23+
24+
export calculate;
2325
}
2426
```
2527

@@ -39,28 +41,39 @@ at the path that is given, and will pull its contents into the local project und
3941

4042
### Calling the import from Rust
4143

42-
Now the declaration of `add` in the adder's WIT file is visible to the `calculator` project. To invoke the imported `add` interface from the `calculate` implementation:
44+
Now the declaration of `add` in the adder's WIT file is visible to the `calculator` project.
45+
46+
To invoke the imported `add` interface from the `calculate` implementation:
4347

4448
```rust
4549
// src/lib.rs
46-
mod bindings;
4750

48-
use bindings::exports::docs::calculator::calculate::Guest;
49-
50-
// Bring the imported add function into scope
51-
use bindings::docs::calculator::add::add;
51+
// Generated code that includes both the import stubs for adder functionality
52+
// and the stubs for exports is generated into the `bindings` module below
53+
//
54+
// Note that while wit_bindgen::generate only creates stubs for imports,
55+
// not implementation -- this component will be built with *unsatisfied*
56+
// (but usable) imports (e.g. the add).
57+
mod bindings {
58+
use super::Component;
59+
wit_bindgen::generate!();
60+
export!(Component);
61+
}
5262

63+
/// The struct from which all implementation will hang
5364
struct Component;
5465

55-
impl Guest for Component {
66+
// Implementation of the `docs:calculator/calculate` export
67+
impl bindings::exports::docs::calculator::calculate::Guest for Component {
5668
fn eval_expression(expr: String) -> u32 {
57-
// Cleverly parse `expr` into values and operations, and evaluate
58-
// them meticulously.
59-
add(123, 456)
69+
// TODO: Cleverly parse `expr` into values and operations, and evaluate them meticulously.
70+
bindings::docs::calculator::add::add(123, 456)
6071
}
6172
}
6273
```
6374

75+
Filling out the implementation of `eval_expression` and actually parsing real expressions is left as an exercise for the reader.
76+
6477
### Fulfilling the import
6578

6679
When you build this using `cargo build`, the `add` interface remains unsatisfied (i.e. imported).

0 commit comments

Comments
 (0)