Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should the drop method take mutable self? #4514

Closed
brson opened this issue Jan 17, 2013 · 6 comments
Closed

Should the drop method take mutable self? #4514

brson opened this issue Jan 17, 2013 · 6 comments
Labels
A-typesystem Area: The type system

Comments

@brson
Copy link
Contributor

brson commented Jan 17, 2013

Every time I write a destructor I think this looks wrong:

impl MyType: Drop {
    fn finalize(&self) {
        // What if I want to mutate myself?!
    }
}

So far I've been able to avoid mutating self in destructors but I must assume there are cases where it's important.

@catamorphism
Copy link
Contributor

Maybe I'm being dense, but why would you want to mutate self when self is about to be freed? Is there any way that such changes would be observable?

@brson
Copy link
Contributor Author

brson commented Jan 17, 2013

That's a good question!

The specific scenario I have in mind is for failure recovery of actors. Imagine you have some type that represents the actor state, and it contains a Port.

struct Actor {
    port: Port<ActorMsg>;
    ... local state ...
}

As an actor, this port is your lifeline to the rest of the world. So the actor is sitting in its task processing events and one of them fails. Most actor systems have one or more recovery and restart strategies. Some of them may preserve the local state and resume processing at the next message, some may assume the state has been corrupted and resume processing messages from scratch.

In Rust I have decided that implementing actor recovery is best done by shipping the Port off to a fresh task, so a scenario where you wanted to recover while wiping out the local state may look like:

impl Actor: Drop {
    fn finalize(&mut self) {
        if failing() {
               let mut saved_port = ... some stand in value ...
               self.port <-> saved_port;
               spawn {
                   Actor::resume(port);
               }
        }
    }
}

@catamorphism
Copy link
Contributor

Thanks for the clear explanation!

@thestinger
Copy link
Contributor

Related: #4330 (taking self by-value in finalize), which would also allow moving out of self

@graydon
Copy link
Contributor

graydon commented May 29, 2013

I'd propose closing this as WONTFIX in favour of #4330 -- while that bug requires us to come up with an extra rule to prevent multiple destruction (eg. "only a destructuring move is allowed in dtor") it also means we don't have to fill destroyable objects with options and permit that many more invalid runtime states.

@brson are you ok with that?

@brson
Copy link
Contributor Author

brson commented May 29, 2013

@graydon yes

@brson brson closed this as completed May 29, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-typesystem Area: The type system
Projects
None yet
Development

No branches or pull requests

4 participants