greentic_interfaces/
component_v0_4.rs

1//! Typed bindings for `greentic:component@0.4.0`.
2
3use wasmtime::Result;
4use wasmtime::StoreContextMut;
5use wasmtime::component::Linker;
6
7mod bindings {
8    wasmtime::component::bindgen!({
9        path: "wit/greentic-component@0.4.0.wit",
10        world: "component",
11    });
12}
13
14pub use self::greentic::component::control::Host as ControlHost;
15pub use self::greentic::component::control::HostWithStore as ControlHostWithStore;
16pub use bindings::*;
17
18/// Registers the Greentic control interface with the provided linker.
19pub fn add_control_to_linker<T>(
20    linker: &mut Linker<T>,
21    get_host: impl Fn(&mut T) -> &mut (dyn ControlHost + Send + Sync + 'static)
22    + Send
23    + Sync
24    + Copy
25    + 'static,
26) -> Result<()>
27where
28    T: Send + 'static,
29{
30    let mut inst = linker.instance("greentic:component/control@0.4.0")?;
31    inst.func_wrap(
32        "should-cancel",
33        move |mut caller: StoreContextMut<'_, T>, (): ()| {
34            let host = get_host(caller.data_mut());
35            let result = host.should_cancel();
36            Ok((result,))
37        },
38    )?;
39    inst.func_wrap(
40        "yield-now",
41        move |mut caller: StoreContextMut<'_, T>, (): ()| {
42            let host = get_host(caller.data_mut());
43            host.yield_now();
44            Ok(())
45        },
46    )?;
47    Ok(())
48}
49
50/// Returns the canonical package name for compatibility checks.
51pub const PACKAGE_ID: &str = "greentic:component@0.4.0";