You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As mentioned in #3, just assigning a Default::default() might not overwrite everything in case the value contains an enum (most probably an Option).
Simply using mem::zeroed is not a good idea if the type contains something for which an all-zero-bits value is invalid.
Possible solution: convert the &mut T to *mut T, and do a memset to zero immediately followed by a ptr::write of the Default::default(), which is valid regardless of dst's contents, since ptr::write ignores the target's contents (it's the official way to initialize a mem::uninitialized()), followed then by the hide_mem. The compiler should be able to elide the memset in the common case where just a Default is enough to overwrite everything.
I'd imagine the reverted commit ccb563e#diff-ac56d72e9f4cbe36b5de6fb90bd33456R33 solves the problem because the ptr::write copies from Default::default() without dropping the value already there, so the only drop should be the *self = mem::zeroed::<Self>(); I'd imagine LLVM would optimizes it to write only once, but not sure.
burdges
added a commit
to burdges/clear_on_drop
that referenced
this issue
Jan 18, 2017
As mentioned in #3, just assigning a
Default::default()
might not overwrite everything in case the value contains anenum
(most probably anOption
).Simply using
mem::zeroed
is not a good idea if the type contains something for which an all-zero-bits value is invalid.Possible solution: convert the
&mut T
to*mut T
, and do amemset
to zero immediately followed by aptr::write
of theDefault::default()
, which is valid regardless ofdst
's contents, sinceptr::write
ignores the target's contents (it's the official way to initialize amem::uninitialized()
), followed then by thehide_mem
. The compiler should be able to elide thememset
in the common case where just aDefault
is enough to overwrite everything.cc #5, @burdges, @bluss
The text was updated successfully, but these errors were encountered: