Skip to main content

host_fn

Macro host_fn 

Source
macro_rules! host_fn {
    ($pub:vis $name: ident  ($($arg:ident : $argty:ty),*) $(-> $ret:ty)? $b:block) => { ... };
    ($pub:vis $name: ident  ($user_data:ident : $dataty:ty; $($arg:ident : $argty:ty),*) $(-> $ret:ty)? $b:block) => { ... };
}
Expand description

The host_fn macro is used to define typed host functions

For example, the following defines a host function named add_newline that takes a string parameter and returns a string result:

extism::host_fn!(add_newline(_user_data: (); a: String) -> String { Ok(a + "\n") });

A few things worth noting:

  • The function always returns a Result that wraps the specified return type
  • If a first parameter and type are passed (_user_data above) followed by a semicolon it will be the name of the UserData parameter and can be used from inside the function