I tried out Rustler and it is amazing when I need to define NIFs at the compile time.
I’m experimenting with a use case where it would be useful to define a NIF at a run time.
Imagine being in an IEx shell where you are able to define a function at the runtime and use it right after.
I wonder how I could do something similar for nifs. Let’s imagine that I have the following Rust code that I want to compile and load at the runtime of BEAM
fn add(a: i64, b: i64) -> i64 {
a + b
}
and use it like
defmodule AdditionWithRust do
def add(a, b), do
add_function = """
fn add(a: i64, b: i64) -> i64 {
a + b
}
"""
RustOnRuntime.apply(add_function, a, b)
end
"""
Where RustOnRuntime
is a fictional module that does some magic.
I would copy paste this to IEx and I would be able to use the function normally.
It’s OK to assume I have a fixed number of Rust libraries that are defined at the compile time, and only those library functions can be used, no new can be loaded.
I suppose this is quite a complex question. I suppose that at the bottom of it there is probably a question if it is even possible to compile and load erlang nifs (of any kind not just Rust) at the run time…
Any pointers what things to consider or where to start would be much appreciated.