[][src]Crate fluence_sdk_macro

This module defines an invocation_handler attribute procedural macro. It can be used to simplify the signature of the main module invocation handler:

use fluence::sdk::*;

#[invocation_handler]
fn greeting(name: String) -> String {
   format!("Hello from Fluence to {}", name)
}

To use this macro with a function f certain conditions must be met:

  1. f shouldn't have more than one input argument.
  2. f shouldn't be unsafe, const, generic, have custom ABI linkage or variadic param.
  3. The type of f input (if it presents) and output parameters should be one from {String, Vec} set.
  4. f shouldn't have the name invoke.

For troubleshooting and macros debugging cargo expand can be used.

Internally this macro creates a new function invoke that converts a raw argument to the appropriate format, calls f and then writes f result via memory::write_response_to_mem to module memory. So to use this crate apart from fluence fluence_sdk_main has to be imported.

The macro also has the init_fn and side_modules attributes. The first one that can be used for specifying initialization function name. This function is called only once at the first call of the invoke function. It can be used like this:

use fluence::sdk::*;
use log::info;

fn init() {
    logger::WasmLogger::init_with_level(log::Level::Info).is_ok()
}

#[invocation_handler(init_fn = init)]
fn greeting(name: String) -> String {
    info!("{} has been successfully greeted", name);
    format!("Hello from Fluence to {}", name)
}

The second macro could be used for generate API to connect with side modules like SQlite and Redis. It can be used like this:

use fluence::sdk::*;

#[invocation_handler(side_modules = (sqlite, redis))]
fn greeting(name: String) -> String {
    sqlite::call("SELECT * from users");
    sqlite::call("GET user");
    format!("Hello from Fluence to {}", name)
}

Examples

Please find more examples here.

Attribute Macros

invocation_handler