I'm implementing a NIF that receives a process pid and a term message. And it will return a reference representing a NIF resource. Once the resource is GCed, it should send the term message it received earlier to the given process pid.
Issues
When implementing this message_on_gc in Rust I have serval issues/questions, and the very first one is,
if I want to hold a value of a NIF term (passed from Elixir, assuming it can be any valid Erlang terms) in LocalMessage, it will have a lifetime 'a, thus LocalMessage should be:
and all subsequent struct that contains a LocalMessage would also have lifetime that is at least 'a. Therefore, if I want to have an ExLocalMessageRef that holds a LocalMessage, it should be:
I'm not sure if I should use lifetime 'static for LocalMessage and ExLocalMessageRef because it doesn't look quite right to me. Plus using 'static is invalid on structs as it is a reserved lifetime name.
I don't quite think that I'm the right track for this... I would greatly appreciate any suggestions anyone may have for me.
Some background on this
I'm implementing a NIF that receives a process
pid
and a termmessage
. And it will return a reference representing a NIF resource. Once the resource is GCed, it should send the termmessage
it received earlier to the given processpid
.Issues
When implementing this
message_on_gc
in Rust I have serval issues/questions, and the very first one is,if I want to hold a value of a NIF term (passed from Elixir, assuming it can be any valid Erlang terms) in
LocalMessage
, it will have a lifetime'a
, thusLocalMessage
should be:and all subsequent struct that contains a
LocalMessage
would also have lifetime that is at least'a
. Therefore, if I want to have anExLocalMessageRef
that holds aLocalMessage
, it should be:As a result, this requires me to mark the
on_load
function with lifetime'a
so that I can userustler::resource!
forExLocalMessageRef<'a>
But the problem is, in
rustler::resource!
, the resource type is declared as static, which requires lifetime'a
to outlive'static
.I'm not sure if I should use lifetime
'static
forLocalMessage
andExLocalMessageRef
because it doesn't look quite right to me. Plus using'static
is invalid on structs as it is a reserved lifetime name.I don't quite think that I'm the right track for this... I would greatly appreciate any suggestions anyone may have for me.