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:
- y..do_thing wants an
X
- 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 ?
erelde
2
Could you use Option::take
?
2 Likes
I prefer to avoid Option
as it incurs a runtime check on every 'normal' usage.
chrefr
4
You can use ManuallyDrop
with ManuallyDrop::take()
. This fundamentally cannot be safe since the field is still dropped after.
2 Likes
alice
5
There is no safe way to do this.
1 Like
jonh
6
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 ?
alice
9
Yes.
I don't think UdpSocket
has a dummy value. You could create a new socket, but that seems somewhat silly.
system
Closed
10
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.