hdk::prelude

Macro map_extern

Source
macro_rules! map_extern {
    ( $name:tt, $f:ident, $input:ty, $output:ty ) => { ... };
}
Expand description

Hides away the gross bit where we hook up integer pointers to length-prefixed guest memory to serialization and deserialization, and returning things to the host, and memory allocation and deallocation.

A lot of that is handled by the holochain_wasmer crates but this handles the boilerplate of writing an extern function as they have awkward input and output signatures:

  • requires remembering #[no_mangle]
  • requires remembering pub extern “C”
  • requires juggling GuestPtr on the input and output with the memory/serialization
  • doesn’t support Result returns at all, so breaks things as simple as ?

This can be used directly as map_extern!(external_facing_fn_name, internal_fn_name) but it is more idiomatic to use the #[hdk_extern] attribute

#[hdk_extern]
pub fn foo(foo_input: FooInput) -> ExternResult<FooOutput> {
 // ... do stuff to respond to incoming calls from the host to "foo"
}