-
-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Add missing mut to pin.rs docs #150705
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
base: main
Are you sure you want to change the base?
Add missing mut to pin.rs docs #150705
Conversation
Per my understanding, needed for mut access next line.
|
Punting on pin, |
|
Sure, though this isn't intended to compile anyway... |
Only because Also, I figured the argument to This compiles: use std::cell::RefCell;
use std::pin::Pin;
trait RefCellPinExt<'a, T> {
fn get_pin_mut(self) -> Pin<&'a mut T>;
}
impl<'a, T> RefCellPinExt<'a, T> for Pin<&'a mut RefCell<T>> {
fn get_pin_mut(self) -> Pin<&'a mut T> {
// SAFETY: That's actually unsafe. Don't do this in real code!
unsafe { self.map_unchecked_mut(|rc| rc.get_mut()) }
}
}
fn exploit_ref_cell<T>(mut rc: Pin<&mut RefCell<T>>) {
// Here we get pinned access to the `T`.
let _: Pin<&mut T> = rc.as_mut().get_pin_mut();
// And here we have `&mut T` to the same data.
let shared: &RefCell<T> = rc.into_ref().get_ref();
let mut borrow = shared.borrow_mut();
let content = &mut *borrow;
}Maybe we better replace it with this version? It's longer, but the compiler can check that it compiles. It's incorrect and unsafe, but having it compile makes sure the example is not bogus. |
Updated to code that compiles
|
|
|
Updated to code that compiles, LMK WDYT |
Per my understanding, needed for mut access next line.