Skip to main content

init

Macro init 

Source
init!() { /* proc-macro */ }
Expand description

Declare the NIF library: its module name, exported NIFs, pre-interned atoms, resource types, and lifecycle callbacks. Invoke it exactly once per library.

It generates the nif_init entry point, the load/upgrade/unload scaffolding (which installs the private-data slot, registers resource types, and interns the declared atoms — all under a catch_unwind), and the __otter_atoms table that the atom! macro reads.

§Syntax

otter::init!(
    "my_module",            // the Erlang module name (required, first)
    [add, subtract],        // the NIF table — functions marked #[otter::nif]
    atoms = [ok, error, not_found = "not found"],
    resources = [MyResource, Conn: "conn-v1"],
    load = on_load,         // optional lifecycle callbacks
    upgrade = on_upgrade,
    unload = on_unload,
);

The first two arguments are positional; everything after is an order-independent keyword entry:

  • atoms = [name, alias = "beam name", …] — atoms interned once at load (and re-interned on upgrade). Use alias = "…" when the BEAM atom text is not a valid Rust identifier. Names are length-checked (≤255 chars) at compile time. Retrieve with the atom! macro.
  • resources = [Type, Type: "tag", …] — resource types to register. A bare Type is registered under an ABI-fingerprinted name (no cross-build takeover); Type: "tag" registers under a stable tagged name that opts into hot-upgrade takeover.
  • load = f / upgrade = f / unload = f — lifecycle callbacks. load and upgrade receive the env and the decoded load_info term and return bool (return false to veto the load); unload receives the env and cannot veto. Their load_raw / upgrade_raw / unload_raw variants (require the raw feature) additionally hand you the library’s user_priv_data void* to manage yourself.
  • allow_panic_abort — a bare flag that opts out of the compile-time guard which otherwise rejects building this crate with panic = "abort" (abort would bypass otter’s panic interception and crash the VM).