Macro smol_macros::main

source ·
macro_rules! main {
    (
        $(#[$attr:meta])*
        async fn $name:ident () $(-> $ret:ty)? $bl:block
    ) => { ... };
    (
        $(#[$post_attr:meta])*
        async fn $name:ident ($ex:ident : & $exty:ty)
        $(-> $ret:ty)? $bl:block
    ) => { ... };
    (
        $(#[$post_attr:meta])*
        async fn $name:ident ($ex:ident : $exty:ty)
        $(-> $ret:ty)? $bl:block
    ) => { ... };
}
Expand description

Turn a main function into one that runs inside of a self-contained executor.

The function created by this macro spawns an executor, spawns threads to run that executor on (if applicable), and then blocks the current thread on the future.

§Examples

Like tokio::main, this function is not limited to wrapping the program’s entry point. In a mostly synchronous program, it can wrap a self-contained async function in its own executor.

use macro_rules_attribute::apply;
use smol_macros::{main, Executor};

fn do_something_sync() -> u32 {
    1 + 1
}

#[apply(main!)]
async fn do_something_async(ex: &Executor<'_>) -> u32 {
    ex.spawn(async { 1 + 1 }).await
}

fn main() {
    let x = do_something_sync();
    let y = do_something_async();
    assert_eq!(x + y, 4);
}

The first parameter to the main function can be an executor. It can be one of the following: