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
Copy file name to clipboardExpand all lines: component-model/src/language-support/using-wit-resources/rust.md
+45-25Lines changed: 45 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,14 @@
2
2
3
3
[Resources](../design/wit.md#resources) are handles to entities that live outside the component (i.e. in a host, or other component).
4
4
5
-
## An example stack-based calculator
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.) A RPN calculator is a stateful entity: a consumer pushes operands and operations onto a stack maintained within the calculator, then evaluates the stack to produce a value. The resource in WIT looks like this:
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.)
8
+
9
+
A RPN calculator is a stateful entity: a consumer pushes operands and operations onto a stack
10
+
maintained within the calculator, then evaluates the stack to produce a value.
11
+
12
+
In WIT, the resource looks like the following:
8
13
9
14
```wit
10
15
package docs:rpn@0.1.0;
@@ -32,7 +37,7 @@ world calculator {
32
37
33
38
## Implementing and exporting a resource in a component
34
39
35
-
To implement the calculator using `cargo component`:
40
+
To implement the calculator in Rust:
36
41
37
42
1. Create a library component as shown in previous sections, with the WIT given above.
38
43
@@ -48,9 +53,15 @@ To implement the calculator using `cargo component`:
48
53
49
54
> Whyisthestackwrappedina `RefCell`?Aswewillsee, thegeneratedRusttraitforthecalculatorenginehas_immutable_referencesto `self`.Butourimplementationofthattraitwillneedtomutatethestack.Soweneedatypethatallowsforinteriormutability, suchas `RefCell<T>` or `Arc<RwLock<T>>`.
@@ -86,23 +97,26 @@ To implement the calculator using `cargo component`:
86
97
4.Wenowhaveaworkingcalculatortypewhichimplementsthe `engine` contract, butwemuststillconnectthattypetothe `engine` resourcetype.Thisisdonebyimplementingthegenerated `Guest` trait.ForthisWIT, the `Guest` traitcontainsnothingexceptanassociatedtype.Youcanuse an empty `struct` to implement the `Guest` trait on.Set the associated typefor the resource -in our case, `Engine` - to the type which implements the resource trait-in our case, the `CalcEngine` `struct` which implements `GuestEngine`.Thenuse the `export!` macro to export the mapping:
0 commit comments