adjust for Weak::as_raw -> as_ptr rename

This commit is contained in:
Ralf Jung 2020-04-20 10:04:17 +02:00
parent 5d2c8358f8
commit 73772fede6

View File

@ -5,17 +5,17 @@
use std::rc::{Rc, Weak};
use std::ptr;
/// Taken from the `Weak::as_raw` doctest.
/// Taken from the `Weak::as_ptr` doctest.
fn main() {
let strong = Rc::new(Box::new(42));
let weak = Rc::downgrade(&strong);
// Both point to the same object
assert!(ptr::eq(&*strong, Weak::as_raw(&weak)));
assert!(ptr::eq(&*strong, Weak::as_ptr(&weak)));
// The strong here keeps it alive, so we can still access the object.
assert_eq!(42, **unsafe { &*Weak::as_raw(&weak) });
assert_eq!(42, **unsafe { &*Weak::as_ptr(&weak) });
drop(strong);
// But not any more. We can do Weak::as_raw(&weak), but accessing the pointer would lead to
// undefined behaviour.
assert_eq!(42, **unsafe { &*Weak::as_raw(&weak) }); //~ ERROR dereferenced after this allocation got freed
assert_eq!(42, **unsafe { &*Weak::as_ptr(&weak) }); //~ ERROR dereferenced after this allocation got freed
}