Skip to content

Should the drop method take mutable self? #4514

Closed

Description

@brson

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.

Activity

  1. catamorphism commented on Jan 17, 2013

    @catamorphism
    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?

  2. brson commented on Jan 17, 2013

    @brson
    ContributorAuthor

    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);
                   }
            }
        }
    }
    
  3. catamorphism commented on Jan 17, 2013

    @catamorphism
    Contributor

    Thanks for the clear explanation!

  4. thestinger commented on Jan 18, 2013

    @thestinger
    Contributor

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

  5. graydon commented on May 29, 2013

    @graydon
    Contributor

    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?

  6. brson commented on May 29, 2013

    @brson
    ContributorAuthor
  7. added a commit that references this issue on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions