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

add as_mut to mutably access owned or mutably borrowed UnsafeCell safely #379

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

datdenkikniet
Copy link

@datdenkikniet datdenkikniet commented Mar 23, 2025

See title.

This is the equivalent to core::cell::UnsafeCell::get_mut, but with tracking. Instead of forcing unnecessary unsafe on users, we can simply give them mutable access to an owned value.

An example of where this causes unnecessary unsafe to be used can be found here.

///
/// This is analogous to [`core::cell::UnsafeCell::get_mut`].
#[track_caller]
pub fn as_mut(&mut self) -> MutRef<'_, T> {
Copy link
Author

Choose a reason for hiding this comment

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

I'm not entirely certain we even need tracking: if we can mutably borrow the value, we already have exclusive access to it, right? And because we borrow it mutably, no one can re-borrow it immutably until the borrow is dropped.

I've added the in-between struct so that someone can double-check my thoughts, but would be great to get some external input on it.

@datdenkikniet datdenkikniet force-pushed the unsafe-cell-as-mut branch 2 times, most recently from ff31094 to f6fb23d Compare March 23, 2025 10:24
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.

1 participant