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

Remove weird Cell example from InvariantType docs (attempt #2) #21995

Merged
merged 2 commits into from
Feb 7, 2015
Merged

Remove weird Cell example from InvariantType docs (attempt #2) #21995

merged 2 commits into from
Feb 7, 2015

Conversation

leejunseok
Copy link
Contributor

Should fix #20147

This is my second PR in the history of ever (I botched my first one #21828). Any tips would be appreciated!

@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 @nikomatsakis (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 CONTRIBUTING.md for more information.

@steveklabnik
Copy link
Member

@bors: r+ 310ada0

/// The type system would infer that `value` is only read here
/// and never written, but in fact `Cell` uses unsafe code to achieve
/// interior mutability. In order to get correct behavior, the
/// `InvariantType` marker must be applied.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not strictly true. That is, all types must use unsafe code to achieve interior mutability. And if they do so, types should use an InvariantType marker (though this is likely to be replaced; see RFC rust-lang/rfcs#738). The only exception to that last rule is Cell, which has no marker because it is builtin to the compiler.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let me put this another, hopefully clearer, way:

  1. UnsafeCell and its wrapper Cell are the best ways to have interior mutability. If you use those, you do not need any markers.
  2. Cell types are required if you are making some data that is truly interior to your struct (i.e., no pointer direction required to reach it) mutable. This is because the compiler has to know whether the interior of a struct is mutable for various safety checks relating to static data.
  3. You may need a marker if you have a pointer like *mut T that gets transmuted or cast in your code to *mut U and that U is mutated when only reachable through a shared reference. In that case, you want a marker InvariantType<U>, because the fact that U will be mutated cannot be observed from the type definition alone -- the compiler sees *mut T, but doesn't know that it will eventually be transmuted to *mut U.

I guess it depends on how one defined "interior" mutability. The only legal way to have some data that is contained within your struct be mutable via a shared reference is to use UnsafeCell, in which case

@bors
Copy link
Contributor

bors commented Feb 7, 2015

⌛ Testing commit 310ada0 with merge a08504b...

bors added a commit that referenced this pull request Feb 7, 2015
Should fix #20147

This is my second PR in the history of ever (I botched my first one #21828). Any tips would be appreciated!
@bors bors merged commit 310ada0 into rust-lang:master Feb 7, 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.

Documentation example for InvariantType is strange
5 participants