@@ -122,73 +122,6 @@ $ cargo run --release -- 1 2 ../add/target/wasm32-wasip1/release/add.wasm
1221221 + 2 = 3
123123```
124124
125- ## Exporting an interface with ` cargo component `
126-
127- While it is possible to export a raw function, it is a best practice to export an interface, similar to the [ sample ` adder ` world] [ add-wit ] .
128- This often makes it easier to comply with an existing specification or to capture a set of functions and types
129- that tend to go together.
130-
131- For example, consider the following changes to the ` add ` interface in the ` adder ` world:
132-
133- ``` wit
134- 135-
136-
137- interface add {
138- variant operand {
139- unsigned32(u32)
140- signed32(s32)
141- zero,
142- }
143-
144- add: func(x: operand, y: operand) -> result<operand, string>;
145- }
146-
147- world adder {
148- export add;
149- }
150- ```
151-
152- This would be implemented with the following Rust code:
153-
154- ``` rust
155- #[allow(warnings)]
156- mod bindings ;
157-
158- use bindings :: exports :: docs :: adder :: add :: {Guest , Operand };
159-
160- struct Component ;
161-
162- impl Guest for Component {
163- fn add (x : Operand , y : Operand ) -> Result <Operand , String > {
164- let x = convert_operand (x );
165- let y = convert_operand (y );
166- match x + y {
167- v if v == 0 => Ok (Operand :: Zero ),
168- v if v < 0 => i32 :: try_from (v )
169- . map_err (| e | format! (" unexpectedly invalid u32: {e}" ))
170- . map (Operand :: Signed32 ),
171- v => u32 :: try_from (v )
172- . map_err (| e | format! (" unexpectedly invalid u32: {e}" ))
173- . map (Operand :: Unsigned32 ),
174- }
175- }
176- }
177-
178- fn convert_operand (operand : Operand ) -> i64 {
179- match operand {
180- Operand :: Unsigned32 (v ) => v as i64 ,
181- Operand :: Signed32 (v ) => v as i64 ,
182- Operand :: Zero => 0 ,
183- }
184- }
185-
186- bindings :: export! (Component with_types_in bindings );
187- ```
188-
189- The code above is unlikely to appear in real interfaces; however, it shows the more common case of an interface
190- including types that are meant to be used * with* the interface in question.
191-
192125## Importing an interface with ` cargo component `
193126
194127The world file (` wit/world.wit ` ) generated for you by ` cargo component new --lib ` doesn't specify any imports.
0 commit comments