]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/rc_as_raw.rs
test weak_into_raw
[rust.git] / tests / compile-fail / rc_as_raw.rs
1 #![feature(weak_into_raw)]
2
3 use std::rc::{Rc, Weak};
4 use std::ptr;
5
6 /// Taken from the `Weak::as_raw` doctest.
7 fn main() {
8     let strong = Rc::new(Box::new(42));
9     let weak = Rc::downgrade(&strong);
10     // Both point to the same object
11     assert!(ptr::eq(&*strong, Weak::as_raw(&weak)));
12     // The strong here keeps it alive, so we can still access the object.
13     assert_eq!(42, **unsafe { &*Weak::as_raw(&weak) });
14     
15     drop(strong);
16     // But not any more. We can do Weak::as_raw(&weak), but accessing the pointer would lead to
17     // undefined behaviour.
18     assert_eq!(42, **unsafe { &*Weak::as_raw(&weak) }); //~ ERROR dangling pointer
19 }