wasmtime/runtime/component/bindgen_examples/_7_async.rs
1bindgen!({
2 inline: r#"
3package example:resource-async;
4
5interface logging {
6 enum level {
7 debug,
8 info,
9 warn,
10 error,
11 }
12
13 resource logger {
14 constructor(max-level: level);
15 get-max-level: func() -> level;
16 set-max-level: func(level: level);
17 /// May block if the host's log buffer is full (i.e. backpressure).
18 log: async func(level: level, msg: string);
19 }
20}
21
22world app {
23 import logging;
24 /// Because `logging.logger.log` is async, `run` must also be async; otherwise,
25 /// a trap will occur at runtime if `run` calls `log` and it blocks.
26 export run: async func();
27}
28"#,
29
30 imports: {
31
32 // This is the notation that's used to flag individual methods:
33 //
34 // "example:resource-async/logging.[constructor]logger": trappable,
35 // "example:resource-async/logging.[method]logger.get-max-level": trappable,
36 // "example:resource-async/logging.[method]logger.set-max-level": trappable,
37 // "example:resource-async/logging.[method]logger.log": trappable,
38 //
39 // Note that if one resource method specifies a flag, the remaining resource methods
40 // are no longer covered by e.g. `default: trappable`, and must be flagged
41 // individually.
42 default: trappable,
43 },
44
45 with: {
46 // Specify that our host resource is going to point to the `MyLogger`
47 // which is defined just below this macro.
48 "example:resource-async/logging.logger": MyLogger,
49 },
50});
51
52/// A sample host-defined type which contains arbitrary host-defined data.
53///
54/// In this case this is relatively simple but there's no restrictions on what
55/// this type can hold other than that it must be `'static + Send`.
56pub struct MyLogger {
57 pub max_level: example::resource_async::logging::Level,
58}