Skip to content

Explain UnsafeCell and Interior Mutability #109

Open
@RalfJung

Description

@RalfJung

It seems to be a reoccurring pattern that people are confused about UnsafeCell and interior mutability, what it does, when they need it and when they don't. And this kind of stuff fits squarely in the scope of the Nomicon, so it'd be great to have some material on that!

In particular, one point of confusion seems to be about shared mutability with raw pointers (see e.g. this forum thread): People read "you need UnsafeCell whenever you mutate data behind a shared reference", and then generalize this to "whenever you mutate shared data" and think that they might need UnsafeCell even when the data they mutate is just behind a *mut T. We should point out somewhere that mutating behind a &*mut T is not in contradiction with any aliasing assumptions made by the compiler.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions