Skip to content
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

Improve mem::forget documentation #25861

Merged
merged 1 commit into from
May 30, 2015
Merged

Improve mem::forget documentation #25861

merged 1 commit into from
May 30, 2015

Conversation

tringenbach
Copy link
Contributor

This adds an example from mem::swap, and provides some suggested uses of this
function.

This is my attempt to summarize the answers to a question I asked on reddit http://www.reddit.com/r/rust/comments/37jcul/what_is_forget_for/ and add the answers to the documentation so that no one else has to google or ask the question again.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @brson (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

/// There's only a few reasons to use this function. The mainly come
/// up in unsafe code or FFI code.
///
/// * You have an uninitialed value, perhaps for performance reasons, and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: "uninitialized"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@steveklabnik
Copy link
Member

Thanks so much for this patch! Just a few nits.

/// let mut t: T = uninitialized();
///
/// // Perform the swap, `&mut` pointers never alias
/// ptr::copy_nonoverlapping(&*x, &mut t, 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this code example will need to import std::ptr to compile, as well as change the call to uninitialized to mem::uninitialized. You can be sure to run the doc tests via make check-stage1-doc-crate-core.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, it would be nice to split up the different examples into different code blocks. right now, the whole thing is no_run because of the file example, but the others can be run.

@tringenbach
Copy link
Contributor Author

@alexcrichton @steveklabnik Ok, I broke it up into 3 code blocks. The last one is still failing when I run make check-stage1-doc-create-core. How do I find out what the error is?

@alexcrichton
Copy link
Member

@tringenbach what's the output of that make command? The error should be buried within there somewhere.

@steveklabnik
Copy link
Member

@tringenbach if you push that commit up to github, I can check it out too :)

@tringenbach
Copy link
Contributor Author

@steveklabnik Fair enough. I was trying to avoid pushing something I knew was broken. It's pushed now.

@alexcrichton It just says that forget_2 failed.

[...]
test marker::PhantomData_2 ... ok
test marker::Reflect_0 ... ok
test mem::forget_1 ... ok
test mem::forget_2 ... FAILED
test mem::forget_0 ... ok
test mem::drop_0 ... ok
test mem::align_of_0 ... ok
[...]
failures:

failures:
mem::forget_2

test result: FAILED. 436 passed; 1 failed; 5 ignored; 0 measured

@steveklabnik
Copy link
Member

You forgot a space before your closing backticks. I have one more nit, so I sent you tringenbach#1 😄

Merge that in, squash this into one commit with rebase, and it's good to go as far as I'm concerned. Sorry there was so much bikeshedding on your first contribution, thank you for improving the docs!

@tringenbach
Copy link
Contributor Author

@steveklabnik Even after merging your pull request, I still get a failure:

test mem::forget_2 ... FAILED

That is on running make check-stage1-doc-crate-core.

@steveklabnik
Copy link
Member

Ah! I wonder how I didn't see this before. It's an easy fix. The issue is

---- mem::forget_2 stdout ----
    <anon>:5:5: 20:6 error: visibility has no effect inside functions
<anon>:5     pub fn swap<T>(x: &mut T, y: &mut T) {
<anon>:6         unsafe {
<anon>:7             // Give ourselves some scratch space to work with
<anon>:8             let mut t: T = mem::uninitialized();
<anon>:9     
<anon>:10             // Perform the swap, `&mut` pointers never alias
          ...
error: aborting due to previous error
thread 'mem::forget_2' panicked at 'Box<Any>', /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libsyntax/diagnostic.rs:211

So, just remove the pub. That gives me no failures, but then again, I had no failures when I sent that PR, so...

This adds an example from mem::swap, and provides some suggested uses of this
function.

Change wording on comment on forget line to be more specific as to why we
need to call forget.

This breaks the examples up into three pieces. The last piece isn't
compiling for some reason.
@tringenbach
Copy link
Contributor Author

@steveklabnik Probably because I didn't originally have the "pub", but added it in a later commit to try to make it work.

How did you get that more detailed out?

Anyway, I made the fix, no more failures, so I squashed it into one commit and did a 'git push -f'.

@steveklabnik
Copy link
Member

@bors: r+ rollup

@bors
Copy link
Contributor

bors commented May 29, 2015

📌 Commit 8746b1a has been approved by steveklabnik

steveklabnik added a commit to steveklabnik/rust that referenced this pull request May 29, 2015
This adds an example from mem::swap, and provides some suggested uses of this
function.

This is my attempt to summarize the answers to a question I asked on reddit http://www.reddit.com/r/rust/comments/37jcul/what_is_forget_for/ and add the answers to the documentation so that no one else has to google or ask the question again.
steveklabnik added a commit to steveklabnik/rust that referenced this pull request May 29, 2015
This adds an example from mem::swap, and provides some suggested uses of this
function.

This is my attempt to summarize the answers to a question I asked on reddit http://www.reddit.com/r/rust/comments/37jcul/what_is_forget_for/ and add the answers to the documentation so that no one else has to google or ask the question again.
bors added a commit that referenced this pull request May 29, 2015
@bors bors merged commit 8746b1a into rust-lang:master May 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants