-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add doc example for std::ptr::drop_in_place
.
#34888
Conversation
I'm not convinced this is the best example, so if someone has a suggestion for improvement here, let me know! |
This is a really terrible example. The biggest problem is that the behavior will change once we transition to MIR and get rid of the old drop flag infrastructure. The other issue is that it's not clear what the code is expected to do without actually running it; using assert! is usually better. A good example would probably involve using malloc() and ptr::write(). |
@eefriedman Thanks for your feedback!
Will this example run differently once the transition to MIR happens? If so, in what ways?
How about something like this? use std::ptr::drop_in_place;
struct SomeStruct {
pub destructor_ran: bool,
};
impl Drop for SomeStruct {
fn drop(&mut self) {
self.destructor_ran = true;
}
}
{
let mut some_struct = SomeStruct {
destructor_ran: false,
};
assert!(!some_struct.destructor_ran);
unsafe {
drop_in_place(&mut some_struct);
}
assert!(some_struct.destructor_ran);
} |
Because of that, we should avoid any example which involves a variable allocated on the stack. |
From what I can tell, |
Oh, right, drop_in_place doesn't do drop filling, sorry, I got confused. It still makes for a very confusing example that we're dropping the same value twice. |
After giving it some thought, and learning more about |
No description provided.