Attribute Macro rustler_codegen::nif

source ·
#[nif]
Expand description

Wrap a function in a Native Implemented Function (NIF) implementation, so that it can be called from Elixir, with all encoding and decoding steps done automatically.

#[nif]
fn add(a: i64, b: i64) -> i64 {
    a + b
}

For functions that may take some time to return - let’s say more than 1 millisecond - it is recommended to use the schedule flag. This tells the BEAM to allocate that NIF call to a special scheduler. These special schedulers are called “dirty” schedulers.

We can have two types of “lengthy work” functions: those that are CPU intensive and those that are IO intensive. They should be flagged with “DirtyCpu” and “DirtyIo”, respectively.

See: https://www.erlang.org/doc/man/erl_nif.html#lengthy_work

#[nif(schedule = "DirtyCpu")]
pub fn my_lengthy_work() -> i64 {
    let duration = Duration::from_millis(100);
    std::thread::sleep(duration);

    42
}