Can drop handler take ownership of a field?

struct Foo {
  x: X,
  y: Y,
}

impl Y {
  pub fn do_thing(&self, x: X) {}
}
impl Drop for Foo {
  pub fn drop(&mut self) {
    self.y.do_thing(self.x);
  }
}

This does not work right now because:

  1. y..do_thing wants an X
  2. the drop handler has a &mut self, so at best can can provide a &mut X

However, this is the drop handler, is there a way to some pull an X out of this ?

Could you use Option::take ?

2 Likes

I prefer to avoid Option as it incurs a runtime check on every 'normal' usage.

You can use ManuallyDrop with ManuallyDrop::take(). This fundamentally cannot be safe since the field is still dropped after.

1 Like

There is no safe way to do this.

1 Like

An alternative to Option is having a dummy/default value and swapping memory.

Does this exist for std::net::UdpSocket? I can't seem to figure out how to construct this value except via ::bind.

Is this the ManuallyDrop::take solution ?

Yes.

I don't think UdpSocket has a dummy value. You could create a new socket, but that seems somewhat silly.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.