Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAllow destructors to be run directly on a memory location, so it is not impossible to handle location-dependent values #753
Comments
rust-highfive
referenced this issue
Jan 27, 2015
Closed
Allow destructors to be run directly on a memory location, so it is not impossible to handle location-dependent values #16242
nrc
changed the title
[CLOSED] Allow destructors to be run directly on a memory location, so it is not impossible to handle location-dependent values
Allow destructors to be run directly on a memory location, so it is not impossible to handle location-dependent values
Jan 27, 2015
This comment has been minimized.
This comment has been minimized.
|
Shouldn't "destroy_directly" take a raw pointer rather than a reference? A reference implies the value is being borrowed, which isn't the case here. |
This comment has been minimized.
This comment has been minimized.
|
I originally gave it that signature to match |
This comment has been minimized.
This comment has been minimized.
|
This seems to be https://doc.rust-lang.org/nightly/std/ptr/fn.drop_in_place.html, so closing. |
scottmcm
closed this
Jan 19, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rust-highfive commentedJan 27, 2015
Monday Aug 04, 2014 at 12:52 GMT
For earlier discussion, see rust-lang/rust#16242
This issue was labelled with: in the Rust repository
Currently, the only way for a library to run a destructor on a value is to let it fall out of scope while on the stack, that is, types wrapping raw pointers (like
VecandRc) have to callptr::readand let the resulting value drop to destroy their contents. This moves values in memory, and thus makes it literally impossible to useVecorRc(or any other library defined container type) for types with destructors that depend on the location of the value in memory.This could be avoid we had a
destroy_directly(&mut T)intrinsic that destroyed that memory location, and so what is currently writtenptr::read(x)(ordrop(ptr::read(x))) would be writtendestroy_directly(&mut *x)and (for types withDropimpls) effectively be just calling(&mut *x).drop().e.g. https://github.com/rust-lang/rust/blob/795f6ae829ab1bfd72394a5da9096e2717ec0f62/src/libcollections/vec.rs#L1539 (there's a variety of other
ptr::reads for a similar purpose in this file, and in other low-level code).As a real-world example, putting a series of
std::comm::Handleinto a preallocatedVec(to satisfy the requirement that they must not move in memory after calling.add) will still break, because the destructors asserts that the memory address is the same as it was and thusVecmoving things in memory upsets it. The value being location dependent is bad, but it seems very reasonable to just destroy things in place to allow it to mostly work (it's probably more efficient too, since I believe LLVM can't optimise away thememcpyout of theVecin all situations).(NB. one can currently work-around this by
box-ing everything, because the compiler-magic of theBoxtype means its destructor destroys things in-place.)