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

Make Arc cloning mechanics clearer in module docs #53782

Merged
merged 3 commits into from
Sep 1, 2018

Conversation

rask
Copy link
Contributor

@rask rask commented Aug 29, 2018

Add some more wording to module documentation regarding how
Arc::clone() works, as some users have assumed cloning Arc's
to work via dereferencing to inner value as follows:

use std::sync::Arc;

let myarc = Arc::new(1);
let myarcref = myarc.clone();

assert!(1 == myarcref);

Instead of the actual mechanic of referencing the existing
Arc value:

use std::sync::Arg;

let myarc = Arc::new(1);
let myarcref = myarc.clone();

assert!(myarcref == &myarc); // not sure if assert could assert this in the real world

Add some more wording to module documentation regarding how
`Arc::clone()` works, as some users have assumed cloning Arc's
to work via dereferencing to inner value as follows:

    use std::sync::Arc;

    let myarc = Arc::new(1);
    let myarcref = myarc.clone();

    assert!(1 == myarcref);

Instead of the actual mechanic of referencing the existing
Arc value:

    use std::sync::Arg;

    let myarc = Arc::new(1);
    let myarcref = myarc.clone();

    assert!(myarcref == &myarc); // not sure if assert could assert this
    in the real world
@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 @cramertj (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. Due to 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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 29, 2018
/// a new pointer to the same value in the heap. When the last `Arc`
/// pointer to a given value is destroyed, the pointed-to value is
/// also destroyed.
/// a new pointer to the same `Arc` reference value in the heap. When the last
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be more accurate to say "produces a new Arc pointing to the same value in the heap". The source might make it clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ooh it works like that! So the internal reference is cloned. Been hearing varying descriptions on how it works. I can change it to reflect that soon. Thanks for clarifying.

@@ -107,7 +107,8 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
/// // The two syntaxes below are equivalent.
/// let a = foo.clone();
/// let b = Arc::clone(&foo);
/// // a and b both point to the same memory location as foo.
/// // a and b both point to the same memory location where foo resides
/// // (not where the value wrapped by foo resides).
Copy link
Contributor

@durka durka Aug 29, 2018

Choose a reason for hiding this comment

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

In fact a, b, and foo are all separate Arcs pointing to the memory location (where the Vec resides (okay actually a couple bytes away from it, to make room for the reference counts)). So I don't think this change is necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As per your other comment, I agree, will remove this.

Make it clearer that `Arc::clone()` in fact creates a whole new
Arc with the internal pointer pointing to the same location as
the source Arc.
/// a new `Arc` instance, which points to the same value on the heap as the
/// source `Arc`, while increasing a reference count. When the last `Arc`
/// pointer to a given value is destroyed, the pointed-to value is also
/// destroyed.
Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds better now!

@@ -107,7 +108,7 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
/// // The two syntaxes below are equivalent.
/// let a = foo.clone();
/// let b = Arc::clone(&foo);
/// // a and b both point to the same memory location as foo.
/// // a and b both point to the same memory location as foo
Copy link
Contributor

Choose a reason for hiding this comment

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

If this still sounds confusing you could say "a and b both point to the same memory location as foo does" or "a, b, and foo all point to the same memory location"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, I probably should make that change to make it even clearer. I know for a fact that folks with English as a second language do appreciate the extra wording.

@rask
Copy link
Contributor Author

rask commented Aug 31, 2018

Are there guidelines on inline documentation formatting? Seems like parts of the documentation block I've been touching here are limited to 80 characters per line while others are limited to 100 characters?

EDIT: found this: https://github.com/rust-lang-nursery/fmt-rfcs/blob/master/guide/guide.md#comments

@durka
Copy link
Contributor

durka commented Aug 31, 2018 via email

@cramertj
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Aug 31, 2018

📌 Commit bf7e324 has been approved by cramertj

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 31, 2018
kennytm added a commit to kennytm/rust that referenced this pull request Sep 1, 2018
…amertj

Make Arc cloning mechanics clearer in module docs

Add some more wording to module documentation regarding how
`Arc::clone()` works, as some users have assumed cloning Arc's
to work via dereferencing to inner value as follows:

    use std::sync::Arc;

    let myarc = Arc::new(1);
    let myarcref = myarc.clone();

    assert!(1 == myarcref);

Instead of the actual mechanic of referencing the existing
Arc value:

    use std::sync::Arg;

    let myarc = Arc::new(1);
    let myarcref = myarc.clone();

    assert!(myarcref == &myarc); // not sure if assert could assert this in the real world
bors added a commit that referenced this pull request Sep 1, 2018
Rollup of 9 pull requests

Successful merges:

 - #53076 (set cfg(rustdoc) when rustdoc is running on a crate)
 - #53622 (cleanup: Add main functions to some UI tests)
 - #53769 (Also link Clippy repo in the CONTRIBUTING.md file)
 - #53774 (Add rust-gdbgui script.)
 - #53781 (bench: libcore: fix build failure of any.rs benchmark (use "dyn Any"))
 - #53782 (Make Arc cloning mechanics clearer in module docs)
 - #53790 (Add regression test for issue #52060)
 - #53801 (Prevent duplicated impl on foreign types)
 - #53850 (Nuke the `const_to_allocation` query)
@bors bors merged commit bf7e324 into rust-lang:master Sep 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants