Crates.io | erl_nif |
lib.rs | erl_nif |
version | 0.5.0 |
source | src |
created_at | 2021-06-16 15:41:58.236861 |
updated_at | 2022-02-27 17:19:01.340103 |
description | Write Erlang and Elixir NIFs in Rust. |
homepage | https://github.com/tangramdotdev/erl_nif |
repository | https://github.com/tangramdotdev/erl_nif |
max_upload_size | |
id | 410936 |
size | 59,060 |
This crate provides bindings to the erl_nif C API, making it easy to write native extensions for Erlang and Elixir in Rust.
Write your NIF in Rust:
erl_nif::init!(
name: "Elixir.Add",
funcs: [add],
);
#[erl_nif::nif]
fn add(a: u64, b: u64) -> Result<u64, String> {
Ok(a + b)
}
Then load it in Elixir.
defmodule Add do
@on_load {:init, 0}
def init do
path = :filename.join(:code.priv_dir(:add), "libadd")
:ok = :erlang.load_nif(path, nil)
end
def add(_) do
:erlang.nif_error(:nif_not_loaded)
end
end
See the examples folder for a complete example.
To make it easy to move data structures between Rust and Elixir, the erl_nif crate supports integration with serde.
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(rename = "Elixir.Example.Contact")]
struct Contact {
name: String,
email: String,
}
defmodule Example do
defmodule Contact do
defstruct [
:name,
:email
]
end
end