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

"Interior Mutability" should use Mutex, not Cell/RefCell #1512

Closed
djmitche opened this issue Nov 29, 2023 · 2 comments · Fixed by #1683
Closed

"Interior Mutability" should use Mutex, not Cell/RefCell #1512

djmitche opened this issue Nov 29, 2023 · 2 comments · Fixed by #1683
Assignees

Comments

@djmitche
Copy link
Collaborator

Cell and RefCell are pretty advanced for introducing interior mutability -- a mutex is much more familiar territory.

Mention Cell and RefCell in the speaker notes as alternatives to Mutex for more specific situations.

@hurryabit
Copy link
Collaborator

I don't think a mutex is a good idea for this course since we don't handle any form of concurrency here.

A good analogy to explain Cell/RefCell could be the mutable keyword from C++. In combination with a better example, maybe something about caching values or lazy initialisation, this should the concept pretty easy to grasp.

@djmitche
Copy link
Collaborator Author

The argument for Mutex is that it's a pretty familiar way to ensure, at runtime, that only one thing is accessing the value inside. Which is really the crux of interior mutability: you can get an exclusive reference from an immutable reference, but somehow you need to guarantee that is the only one.

Cell just has methods that take &self but actually mutate the value (and references concurrency for the safety of this operation), while RefCell is a new concept.

At any rate, I think the slide should focus on the concept, leaving the particular std type used to achieve the interior mutability as an incidental detail.

djmitche added a commit that referenced this issue Jan 17, 2024
Mutex is probably more broadly understood by people coming from other
languages.

Fixes #1512.
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 a pull request may close this issue.

2 participants