rustler
rustler copied to clipboard
[RFC] Pseudo-reload
As explained in #13 it's not feasible to allow for "real" module unloading for rustler-based nif-modules. However, by (automatically) versioning the nif-modules we could achieve something similar:
In my rebar3_rust
update branch I already place crates in versioned directories and generate a header-file that contains the location of the binaries relative to priv
on build. We could now go one step further and generate a "wrapper" nif module that refers to exactly one version and make the rustler_codegen
generate its name from the crate version and compile mode. A nif crate of name my_nifs
with a single nif func/1
of version 1.0 would then generate a module like:
-module('my_nifs_crate_1.0_release').
-export([func/1]).
-on_load(init/0).
init() -> erlang:load_nif("/path/to/output").
func(Arg1) -> erlang:nif_error(not_loaded).
The actual module to expose this would then be recompiled on version change and look like this, where crates.hrl
is regenerated on build to refer to the respective current generated module:
-module(my_nifs).
-include("crates.hrl").
-spec func(number()) -> ok.
func(MyNumber) ->
?crate_my_nifs:func(MyNumber).
The user-defined my_nifs
module would then, on reload, pick up a new generated module which in turn would load the new so/dll.
Since 'my_nifs_crate_1.0_release'
should be generated on build, we could incorporate it into rustler_codegen
's init!
implementation and even add auto-generated specs
.