guest_function

Attribute Macro guest_function 

Source
#[guest_function]
Expand description

Attribute macro to mark a function as a guest function. This will register the function so that it can be called by the host.

If a name is provided as an argument, that name will be used to register the function. Otherwise, the function’s identifier will be used.

The function arguments must be supported parameter types, and the return type must be a supported return type or a Result<T, HyperlightGuestError> with T being a supported return type.

§Note

The function will be registered with the host at program initialization regardless of the visibility modifier used (e.g., pub, pub(crate), etc.). This means that a private functions can be called by the host from beyond its normal visibility scope.

§Example

use hyperlight_guest_bin::guest_function;
#[guest_function]
fn my_guest_function(arg1: i32, arg2: String) -> i32 {
    arg1 + arg2.len() as i32
}

or with a custom name:

use hyperlight_guest_bin::guest_function;
#[guest_function("custom_name")]
fn my_guest_function(arg1: i32, arg2: String) -> i32 {
    arg1 + arg2.len() as i32
}

or with a Result return type:

use hyperlight_guest_bin::guest_function;
use hyperlight_guest::bail;
#[guest_function]
fn my_guest_function(arg1: i32, arg2: String) -> Result<i32, HyperlightGuestError> {
    bail!("An error occurred");
}