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 interior mutability slide #1683

Merged
merged 7 commits into from
Jan 17, 2024
Merged

Improve interior mutability slide #1683

merged 7 commits into from
Jan 17, 2024

Conversation

djmitche
Copy link
Collaborator

Mutex is probably more broadly understood by people coming from other languages.

Fixes #1512.

Mutex is probably more broadly understood by people coming from other
languages.
@mgeisler
Copy link
Collaborator

Like @hurryabit said in the issue, I also feel it's weird to use a Mutex here: other languages typically don't let the mutex wrap the data like you do in Rust. So being familiar with std::mutex or a similar type might actually confuse people.

The mutex.lock().unwrap() pattern is strange as well and it's certainly something people ask about in the Concurrency class.

Talking about Cell feel equally clear to me: it's a storage location which you can get and set on. It is a very clear concept and it avoids the baggage of thinking about threads.

@djmitche djmitche changed the title Use Mutex as an example of interior mutability Improve interior mutability slide Jan 12, 2024
@djmitche
Copy link
Collaborator Author

I went back to using just RefCell, with the same example, but improved the text and notes a little bit. WDYT?

Copy link
Collaborator

@mgeisler mgeisler left a comment

Choose a reason for hiding this comment

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

Thanks, I think should be good.

@mgeisler
Copy link
Collaborator

I went back to using just RefCell, with the same example, but improved the text and notes a little bit. WDYT?

I think it looks pretty good. I really like your concise part about "if there are no references, borrowing rules cannot be broken". I had no thought of it like this myself.

Have you considered focusing on Cell only? As a way to introduce the main concept of a type T where you can call set on a &T value? To me, that is the essence of interior mutability: the typesystem says you have an shared read-only reference, and yet you can modify something. The precise default of Cell vs RefCell is less important in my opinion.

@djmitche
Copy link
Collaborator Author

I did consider using Cell instead, but I think it's such a "thin" layer that it actually obscures the meaning -- it would be easy to take away an understanding that "interior mutability is this special Cell::set that takes &self but still modifies itself".

I think RefCell hits the sweet spot of being complex enough to illustrate the point, without being too complex.

@mgeisler
Copy link
Collaborator

I did consider using Cell instead, but I think it's such a "thin" layer that it actually obscures the meaning -- it would be easy to take away an understanding that "interior mutability is this special Cell::set that takes &self but still modifies itself".

I think RefCell hits the sweet spot of being complex enough to illustrate the point, without being too complex.

Alright, I'm happy with that. Let's get it in! 😄

@djmitche djmitche merged commit 73068d2 into google:main Jan 17, 2024
31 checks passed
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.

"Interior Mutability" should use Mutex, not Cell/RefCell
2 participants