Macro Spawn

Source
macro_rules! Spawn {
    (|$($param:ident : $type:ty),+ $(,)?| $body:block) => { ... };
    ($fn_name:ident($($arg:expr),* $(,)?)) => { ... };
    (
        |$($param:ident : $type:ty),+ $(,)?| $body:block,
        $(
            $key:ident : $value:expr
            $(,)?
        )*
    ) => { ... };
    (
        $fn_name:ident($($arg:expr),* $(,)?),
        $(
            $key:ident : $value:expr
            $(,)?
        )*
    ) => { ... };
}
Expand description

The Spawn!() macro is defined here as a no-op. However, in practice, kit build will rewrite it during pre-processing.

Examples:

fn init(our: Address) {
    let parent = our.clone();
    Spawn!(|parent: Address| {
        println!("hello from {our}. I am Spawn of {parent}!");
    });
    ...
}

will be rewritten by kit build to:

  1. Generate a new child process within the package that, here, println!()s, or, in general, executes the code given by the closure.
  2. Replace the code lines in the parent process with spawn() to start the generated child and send a [Request()] to pass in the closure’s args.
  3. Update the relevant metadata for the package (i.e. Cargo.toml, metadata.json, etc.).

More example usage:

Can pass function call rather than closure:

fn init(our: Address) {
    let parent = our.clone();
    Spawn!(my_function(parent));
    ...
}

Nested function calls work as expected.

Can optionally supply subset of spawn() arguments, namely

  • name: &str,
  • on_exit: OnExit,
  • request_capabilities: Vec<Capability>,
  • grant_capabilities: Vec<ProcessId>,
  • public: bool, for example:
fn init(our: Address) {
    let parent = our.clone();
    Spawn!(my_function(parent), name: "hello-world", public: true);
    ...
}