You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 4b1ad2f
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: component-model/src/language-support/using-wit-resources/rust.md
+46-14Lines changed: 46 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,8 @@
4
4
5
5
## An example stack-based Reverse Polish Notation (RPN) calculator
6
6
7
-
In this section, our example resource will be a [Reverse Polish Notation (RPN)](https://en.wikipedia.org/wiki/Reverse_Polish_notation) calculator. (Engineers of a certain vintage will remember this from handheld calculators of the 1970s.)
7
+
In this section, our example resource will be a [Reverse Polish Notation (RPN)](https://en.wikipedia.org/wiki/Reverse_Polish_notation)
8
+
calculator. (Engineers of a certain vintage will remember this from handheld calculators of the 1970s.)
8
9
9
10
A RPN calculator is a stateful entity: a consumer pushes operands and operations onto a stack
10
11
maintained within the calculator, then evaluates the stack to produce a value.
@@ -39,7 +40,19 @@ world calculator {
39
40
40
41
To implement the calculator in Rust:
41
42
42
-
1. Create a library component as shown in previous sections, with the WIT given above.
43
+
1.[Create a library component](../building-a-simple-component/rust.md), save the
44
+
WIT above as `wit/world.wit`, and add `wit-bindgen`:
45
+
46
+
```sh
47
+
cargo add wit-bindgen
48
+
```
49
+
50
+
Be sure to include the `cdylib` crate typein the `Cargo.toml` as well:
51
+
52
+
```toml
53
+
[lib]
54
+
crate-type = ["cdylib"]
55
+
```
43
56
44
57
2. Define a Rust `struct` to represent the calculator state:
45
58
@@ -57,9 +70,7 @@ To implement the calculator in Rust:
57
70
58
71
```rust
59
72
mod bindings {
60
-
usesuper::Component;
61
73
wit_bindgen::generate!();
62
-
export!(Component);
63
74
}
64
75
65
76
use bindings::exports::docs::rpn::types::{Guest, GuestEngine, Operation};
@@ -97,14 +108,13 @@ To implement the calculator in Rust:
97
108
4. We now have a working calculator type which implements the `engine` contract, but we must still connect that type to the `engine` resource type. This is done by implementing the generated `Guest` trait. For this WIT, the `Guest` trait contains nothing except an associated type. You can use an empty `struct` to implement the `Guest` trait on. Set the associated typeforthe resource -in our case, `Engine` - to the type which implements the resource trait - in our case, the `CalcEngine``struct` which implements `GuestEngine`. Then use the `export!` macro to export the mapping:
98
109
99
110
```rust
100
-
// ... bindings & CalcEngine impl code ...
101
-
102
111
struct Component;
103
112
104
113
impl Guest for Component {
105
114
type Engine = CalcEngine;
106
115
}
107
116
117
+
bindings::export!(Component);
108
118
```
109
119
110
120
This completes the implementation of the calculator `engine` resource. Run `cargo build --target=wasm32-wasip2` to create a component `.wasm` file.
@@ -139,7 +149,7 @@ To use the calculator engine in another component, that component must import th
139
149
```rust
140
150
mod bindings {
141
151
wit_bindgen::generate!({
142
-
generate_all
152
+
generate_all,
143
153
});
144
154
}
145
155
@@ -160,7 +170,7 @@ Building the component as is creates a WebAssembly component with an *unsatisfie
160
170
After building the component, it must be [composed with a `.wasm` component that implements the resource.](../../composing-and-distributing/composing.md). After composition creates a component with no unsatisfied imports, the composed command component can be run with `wasmtime run`.
161
171
162
172
Alternatively, a host that can provide the `docs:rpn/types` import (and related resource) can also be used to run the component
0 commit comments