rusterlium / erlang_nif-sys

Low level bindings to Erlang NIF API for Rust
Apache License 2.0
90 stars 19 forks source link

What about enif_make_pid? #18

Closed cybernetlab closed 7 years ago

cybernetlab commented 7 years ago

Hi! I can't find enif_make_pid function in your project. Is it possible to add it? Also I've try to add it in my code as a workaround:

extern "C" {
    fn enif_make_pid(env: *mut ErlNifEnv, pid: *const ErlNifPid) -> ERL_NIF_TERM;
}

but it crashes when nif library loading into VM with message Failed to load NIF library: '/.../mylib.so: undefined symbol: enif_make_pid'. Looks like it is a new feature? This is my erl banner: Erlang/OTP 19 [erts-8.1] [source] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false].

Regards

goertzenator commented 7 years ago

It is indeed missing. The original API implements it as a macro, so that's why it was overlooked.

ErlNifPid looks like this...

pub struct ErlNifPid {
    pid: ERL_NIF_TERM,
}

... and all the macro does is return the pid member, so hopefully that gives you grounds for a work-around. Worst case you can transmute the pid into a term.

cybernetlab commented 7 years ago

Actually my question was about function enif_make_pid - not about type ErlNifPid. This function does not implemented in rust library. Are you mean that I can use pid field from this structure as return value from my nif function back to elixir/erlang directly without call to enif_make_pid? As I understand the returning terms should be in environment that passed to nif function - I think that enif_make_pid ensures that this is a true.

goertzenator commented 7 years ago

The C implementation simply yanks the pid member from the structure and returns it. Pid terms are currently environmentless, although the API is designed so that it could be changed in the future (but I don't foresee any overhauls in this department).

I will leave this open so I can add the function later when I'm not knee deep in other stuff. In the meantime I suggest just writing your own version of enif_make_pid that does a transmute.

cybernetlab commented 7 years ago

Thank you for detailed explanation. I think that I can prepare a PR with this function - adding it to gen_api.erl inside right eyes version block should be enough.

goertzenator commented 7 years ago

A PR would be great! A few more points/references:

cybernetlab commented 7 years ago

I've prepared PR.

some remarks: