I'm using gfx-hal
, which requires me to create resources which need to be explicitly destroyed using functions specific to their type. I'd like to store instances of these types in structs, and I'd also like to tie cleaning them up to the lifetime of the owning struct, instead of managing their lifetimes manually and potentially having objects on the GPU/in the driver live forever.
However, all the functions in the destroy
family of functions take the type directly, rather than a reference, so when I try to pass them from my structs, I get errors like the following:
error[E0509]: cannot move out of type `S`, which implements the `Drop` trait
--> src/lib.rs:9:18
|
9 | destroyT(self.member)
| ^^^^^^^^^^^ cannot move out of here
It seems like there should be some way around this issue, as I'm currently in the Drop::drop
function itself, so self
is already "consumed." How do I get the instances of these types out of self
as T
, and not &T
?
struct T;
struct S {
member: T,
}
impl Drop for S {
fn drop(&mut self) {
destroyT(self.member)
}
}
// elsewhere, in a library
fn destroyT(t: T) {
//...
}
The safest, easiest way to do this is to use an
Option
:You could choose to use unsafe code with
ManuallyDrop
and swap out the current value for an uninitialized one1:1 Using
mem::uninitialized
is extremely dangerous and hard to get right, especially in generic contexts. Using the nightlyMaybeUninit
, this might look likeSee also: