Hello rustaceans. Would somebody be kind enough to help me with a NIF problem? I'm using the rustler library to write a NIF for elixir, and I'm getting stuck. The purpose of my library is to implement a table data structure for time series analysis. Each column works like a circular queue. In elixir we define our table dimensions and column labels to instantiate a table, and in rust the table is defined as:
struct SlidingWindow<'a> {
data: Mutex<HashMap<&'a str, Vec<f32>>>,
insertion_index: usize,
length: usize,
width: usize,
labels: &'a [&'a str],
}
I've tried passing this to the rustler::resource!
macro so I can generate references to my table instances for elixir, but this doesn't compile and I suspect this isn't the right way to do what I want (I think this comment describes my situation).
fn load(env: Env, _info: Term) {
rustler::resource!(SlidingWindow, env);
}
It seems that rustler::resource!
is incompatible with structs that require a named lifetime. The macro code is here. When we compile, rust says:
error[E0726]: implicit elided lifetime not allowed here
--> src/lib.rs:177:24
|
177 | rustler::resource!(SlidingWindow, env);
| ^^^^^^^^^^^^^- help: indicate the anonymous lifetime: `<'_>`
...and if we try the suggestion...
error[E0478]: lifetime bound not satisfied
--> src/lib.rs:177:5
|
177 | rustler::resource!(SlidingWindow<'_>, env);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: lifetime parameter instantiated with the lifetime `'_` as defined on the impl at 177:38
--> src/lib.rs:177:38
|
177 | rustler::resource!(SlidingWindow<'_>, env);
| ^^
= note: but lifetime parameter must outlive the static lifetime
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to previous error
I get the feeling that the solution will ultimately be simple, as I'm aiming to give elixir only a reference to my data, not serialize the data into elixir terms, but I don't know how to do this. I think I need to somehow make an ErlNifResourceType
(see erlang.org/doc/man/erl_nif.html
), and implement ResourceTypeProvider
and maybe Encoder
.