|
| 1 | +#![feature(weak_into_raw)] |
| 2 | + |
1 | 3 | use std::cell::{Cell, RefCell};
|
2 |
| -use std::rc::Rc; |
| 4 | +use std::rc::{Rc, Weak}; |
3 | 5 | use std::sync::Arc;
|
4 | 6 | use std::fmt::Debug;
|
5 | 7 |
|
@@ -69,12 +71,46 @@ fn rc_fat_ptr_eq() {
|
69 | 71 | drop(unsafe { Rc::from_raw(r) });
|
70 | 72 | }
|
71 | 73 |
|
| 74 | +/// Taken from the `Weak::into_raw` doctest. |
| 75 | +fn weak_into_raw() { |
| 76 | + let strong = Rc::new(42); |
| 77 | + let weak = Rc::downgrade(&strong); |
| 78 | + let raw = Weak::into_raw(weak); |
| 79 | + |
| 80 | + assert_eq!(1, Rc::weak_count(&strong)); |
| 81 | + assert_eq!(42, unsafe { *raw }); |
| 82 | + |
| 83 | + drop(unsafe { Weak::from_raw(raw) }); |
| 84 | + assert_eq!(0, Rc::weak_count(&strong)); |
| 85 | +} |
| 86 | + |
| 87 | +/// Taken from the `Weak::from_raw` doctest. |
| 88 | +fn weak_from_raw() { |
| 89 | + let strong = Rc::new(42); |
| 90 | + |
| 91 | + let raw_1 = Weak::into_raw(Rc::downgrade(&strong)); |
| 92 | + let raw_2 = Weak::into_raw(Rc::downgrade(&strong)); |
| 93 | + |
| 94 | + assert_eq!(2, Rc::weak_count(&strong)); |
| 95 | + |
| 96 | + assert_eq!(42, *Weak::upgrade(&unsafe { Weak::from_raw(raw_1) }).unwrap()); |
| 97 | + assert_eq!(1, Rc::weak_count(&strong)); |
| 98 | + |
| 99 | + drop(strong); |
| 100 | + |
| 101 | + // Decrement the last weak count. |
| 102 | + assert!(Weak::upgrade(&unsafe { Weak::from_raw(raw_2) }).is_none()); |
| 103 | +} |
| 104 | + |
72 | 105 | fn main() {
|
73 | 106 | rc_fat_ptr_eq();
|
74 | 107 | rc_refcell();
|
75 | 108 | rc_refcell2();
|
76 | 109 | rc_cell();
|
77 | 110 | rc_raw();
|
78 | 111 | rc_from();
|
| 112 | + weak_into_raw(); |
| 113 | + weak_from_raw(); |
| 114 | + |
79 | 115 | arc();
|
80 | 116 | }
|
0 commit comments