#[func]Expand description
Enable a function to be used as an entrypoint of a child process, and turn it into an
Object.
This macro applies to fn functions, including generic ones. It turns the function into an
object that can be called (providing the same behavior as if #[func] was not used), but also
adds various methods for spawning a child process from this function.
For a function declared as
#[func]
fn example(arg1: Type1, ...) -> Output;…the methods are:
pub fn spawn(&self, arg1: Type1, ...) -> std::io::Result<crossmist::Child<Output>>;
pub fn run(&self, arg1: Type1, ...) -> std::io::Result<Output>;For example:
use crossmist::{func, main};
#[func]
fn example(a: i32, b: i32) -> i32 {
a + b
}
#[main]
fn main() {
assert_eq!(example(5, 7), 12);
assert_eq!(example.spawn(5, 7).unwrap().join().unwrap(), 12);
assert_eq!(example.run(5, 7).unwrap(), 12);
}Asynchronous case
This section applies if the tokio feature is enabled.
The following methods are also made available:
pub async fn spawn_tokio(&self, arg1: Type1, ...) ->
std::io::Result<crossmist::tokio::Child<Output>>;
pub async fn run_tokio(&self, arg1: Type1, ...) -> std::io::Result<Output>;Additionally, the function may be async. In this case, you have to add the #[tokio::main]
attribute after #[func]. For instance:
#[crossmist::func]
#[tokio::main]
async fn example() {}You may pass operands to tokio::main just like usual:
#[crossmist::func]
#[tokio::main(flavor = "current_thread")]
async fn example() {}Notice that the use of spawn vs spawn_tokio is orthogonal to whether the function is
async: you can start a synchronous function in a child process asynchronously, or vice versa:
use crossmist::{func, main};
#[func]
fn example(a: i32, b: i32) -> i32 {
a + b
}
#[main]
#[tokio::main(flavor = "current_thread")]
async fn main() {
assert_eq!(example(5, 7), 12);
assert_eq!(example.run_tokio(5, 7).await.unwrap(), 12);
}use crossmist::{func, main};
#[func]
#[tokio::main(flavor = "current_thread")]
async fn example(a: i32, b: i32) -> i32 {
a + b
}
#[main]
fn main() {
assert_eq!(example.run(5, 7).unwrap(), 12);
}