lunatic-rs icon indicating copy to clipboard operation
lunatic-rs copied to clipboard

Spawning processes should allow for borrowed captured data

Open tqwewe opened this issue 3 years ago • 2 comments

Currently, trying to spawn a process requires you to clone captured data if you'd like to use it after the spawn. But because encoding data only requires a & reference, cloning should not be required.

Eg:

#[derive(Serialize, Deserialize)]
struct Foo {} // Doesn't implement clone

fn main() {
    let foo = Foo {};
    
    Process::spawn(&foo, |foo, _: Mailbox<()>| { ... }); // Fails, Deserialize is not implemented for `&Foo`, only `Foo`.
    Process::spawn(foo, |foo, _: Mailbox<()>| { ... });
    Process::spawn(foo, |foo, _: Mailbox<()>| { ... }); // fails, because foo was moved
}

It would be nice if spawn worked with both &foo and foo, so the change would not be breaking in any way.

tqwewe avatar Sep 01 '22 05:09 tqwewe

The issue are resources, they need to be owned when encoded. We could though explicitly clone them during serialisation.

There was also an idea of special serializers that send a reference to the process' memory, so that copies are completely avoided. In this case everything that is sent needs to be consumed, because now some other process owns part of your memory. To be forward compatible, I decided that captured data is owned. Not sure if we are getting this feature soon though.

If you serialize &Foo, can you deserialize it as Foo? What I mean is, from what are you borrowing the Foo inside the other process.

bkolobara avatar Sep 01 '22 07:09 bkolobara

I see, that makes sense if the data really is semantically being moved to another process. I often imagined it was copied with serialization, but forward compatible reasons this makes sense.

tqwewe avatar Sep 06 '22 04:09 tqwewe