-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
4b9d80325a65b0375eea526409a0f3aaf1cbc23c | ||
81970852e172c04322cbf8ba23effabeb491c83c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#![feature(weak_into_raw)] | ||
|
||
use std::rc::{Rc, Weak}; | ||
use std::ptr; | ||
|
||
/// Taken from the `Weak::as_raw` 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))); | ||
// The strong here keeps it alive, so we can still access the object. | ||
assert_eq!(42, **unsafe { &*Weak::as_raw(&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 dangling pointer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters