macro_rules! host_fn {
($name:ident() -> $ret:ty $body:block) => { ... };
($name:ident($a:ident: $at:ty) -> $ret:ty $body:block) => { ... };
($name:ident($a:ident: $at:ty, $b:ident: $bt:ty) -> $ret:ty $body:block) => { ... };
($name:ident($a:ident: $at:ty, $b:ident: $bt:ty, $c:ident: $ct:ty) -> $ret:ty $body:block) => { ... };
($name:ident($a:ident: $at:ty, $b:ident: $bt:ty, $c:ident: $ct:ty, $d:ident: $dt:ty) -> $ret:ty $body:block) => { ... };
(ctx, $name:ident() -> $ret:ty $body:block) => { ... };
(ctx, $name:ident($a:ident: $at:ty) -> $ret:ty $body:block) => { ... };
(ctx, $name:ident($a:ident: $at:ty, $b:ident: $bt:ty) -> $ret:ty $body:block) => { ... };
}Expand description
Macro to define a typed host function.
§Examples
ⓘ
// Simple function without context
host_fn!(add(a: i64, b: i64) -> i64 {
a + b
});
// Function with context access
host_fn!(ctx, log_message(msg: String) -> () {
ctx.record_output(msg.len())?;
println!("{}", msg);
Ok(())
});
// Function with optional argument
host_fn!(greet(name: String, greeting: Optional<String>) -> String {
let greeting = greeting.0.unwrap_or_else(|| "Hello".to_string());
format!("{}, {}!", greeting, name)
});