-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
re-do documentation for Drop #57449
re-do documentation for Drop #57449
Conversation
r? @shepmaster (rust_highfive has picked a reviewer for you, use r? to override) |
src/libcore/ops/drop.rs
Outdated
/// Dropping HasDrop! | ||
/// ``` | ||
/// | ||
/// Similarly, a slice will drop each element in order. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it'd make sense to introduce a subheading ## Drop order
here? I'd imagine people will be often specifically looking for that (the last version that something similar). What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was torn. It seems a bit small, but maybe it is worth it. I'm open to suggestions!
/// | ||
/// ``` | ||
/// | ||
/// When a value goes out of scope, it will call `Drop::drop` on that value. For example, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it will call
What is "it"?
/// call to `Drop::drop`, you'd get a compiler error. | ||
/// | ||
/// If you'd like to call `drop` yourself, there is something you can do: call [`std::mem::drop`]. | ||
/// This function will drop its argument. For more, see its documentation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For more, see its documentation.
Is this required when the function itself is linked?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying to allude to there being more to it, if that makes any sense.
src/libcore/ops/drop.rs
Outdated
/// | ||
/// Which of our two `HasDrop` drops first, though? It's the same order that | ||
/// they're declared: first `one`, then `two`. If you'd like to try this | ||
/// yourself, you can modify `HasDrop` above to contain some data, like an |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can modify
HasDrop
above to contain some data
Should we proactively do that to prove the point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about it but 1. it's long 2. it's not a huge change, and people sometimes like examples they can try. i'm open to including it, it'd just make this section even longer than it already is. maybe that's okay!
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
Fixes rust-lang#36073 Drop's docs were not great, and as mentioned in rust-lang#36073, focused too much on scope. I've re-done them a bit, using the same examples, and making it clear that scope is not the only thing that causes destructors to run.
@steveklabnik I'm not opposed to reviewing this, but is there someone more documentation-minded that would be better? |
Yeah, does someone from @rust-lang/docs want to take this on? |
Mmmm, i think the text here is not really consistent with the Reference. It has been this way for a long time, but i'll really appreciate it if we can fix it. The reference text is something like this:
So you see, |
@crlf0710 i was trying to bring it in line :) The destructor consists of calling |
@steveklabnik I think the distinction is important. Every type has its own destructor. It's just that sometimes the destructor is, in C++ terminology, trivial. The types implementing Drop is a subset of the the types having a nontrivial destructor, which in turn is a subset of all types. |
/// | ||
/// impl Drop for Inner { | ||
/// Our custom implementation of `Drop` will be called at the end of `main`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A little misleading here, it seems as if there's a default implementation of Drop
, which is not true.
/// | ||
/// [`std::mem::drop`]: ../../std/mem/fn.drop.html | ||
/// | ||
/// ## `Drop` is recursive |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's the destructor that is recursive...
/// | ||
/// You cannot implement both [`Copy`] and `Drop` on the same type. Types that | ||
/// are `Copy` don't manage resources, and can be freely copied. As such, they | ||
/// cannot have destructors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rust terminology of "nontrivial destructors"
/// order that they're declared: first `one`, then `two`. If you'd like to try | ||
/// this yourself, you can modify `HasDrop` above to contain some data, like an | ||
/// integer, and then use it in the `println!` inside of `Drop`. This behavior is | ||
/// guaranteed by the language. In other situations, the rules may be different, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basicly there're two drop orders in Rust: by declare order (for structs/arrays), by reverse order (for stack variables). Only talk one of them, but omit another, will mislead people (to think that variables are droped by declare order too).
@rust-lang/lang, what do you think about the above language? |
I think this is a definite improvement, but I agree with the above comments that I think it's important to highlight the difference between running drop glue on a value (which happens to values of every type) and running the |
Thanks @cramertj . The reference doesn't really draw a distinction between "drop glue" and "Drop"; do you think it should there as well? |
@steveklabnik yeah, I do-- I think that's an important distinction to make. I wish we had better language for it, but "drop glue" is what I've heard it called most consistently. |
Cool, thanks! I'll pursue that as well, separate from this ticket. |
I've given this a few tries, and am still not quite happy with it. Given that this PR is going to change significantly thanks to feedback, I'm going to give it a close and try again later. Thanks for the reviews, everyone! |
"drop glue" is now more or less |
…ulacrum improve Drop documentation Fixes rust-lang#36073 This is a continuation of rust-lang#57449 and most of the work here was done by the excellent @steveklabnik.
Fixes #36073
Drop's docs were not great, and as mentioned in #36073, focused too much on scope.
I've re-done them a bit, using the same examples, and making it clear that scope is not
the only thing that causes destructors to run.
I'm not feeling super awesome about the
Copy
vsDrop
section. Anyone have any advice?