Replies: 2 comments
-
The reason this doesn't work is that it's not sound. You can't keep a pub struct LocalMessage {
pid: LocalPid,
env: OwnedEnv,
term: SavedTerm,
}
impl LocalMessage {
pub new<'a>(pid: LocalPid, term: Term<'a>) -> Self {
let env = OwnedEnv::new();
let term = env.save(term);
Self { env, pid, term }
}
// Consumes self
pub fn send(self) -> Result<(), SendError> {
self.env.send_and_clear(|env| env.load(self.term));
}
} It should be possible to put this object into a container in a resource. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Oh now I understand! Thank you very much @filmor! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi all, I'm trying to implement a NIF in Rust but I have serval issues/questions when using rustler. I would greatly appreciate any suggestions anyone may have for me as I don't quite think that I'm the right track for this...
Related PR that attempts to do it in Rust elixir-explorer/explorer#931. Currently it's implemented in C here in elixir-explorer/explorer#930.
Some background on this
I'm writing a
message_on_gc
function which is a NIF that receives a processpid
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
.Core lines from the C implementation:
C Code
Questions/Issues
When implementing it in Rust, the very first issue I have 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 structs 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.Beta Was this translation helpful? Give feedback.
All reactions