File tree Expand file tree Collapse file tree 3 files changed +38
-37
lines changed Expand file tree Collapse file tree 3 files changed +38
-37
lines changed Original file line number Diff line number Diff line change 1- use std:: cell:: RefCell ;
1+ use std:: cell:: { Cell , RefCell } ;
22use std:: rc:: Rc ;
3+ use std:: sync:: Arc ;
34
45fn rc_refcell ( ) {
56 let r = Rc :: new ( RefCell :: new ( 42 ) ) ;
7+ let r2 = r. clone ( ) ;
68 * r. borrow_mut ( ) += 10 ;
7- let x = * r . borrow ( ) ;
9+ let x = * r2 . borrow ( ) ;
810 assert_eq ! ( x, 52 ) ;
911}
1012
13+ fn rc_cell ( ) {
14+ let r = Rc :: new ( Cell :: new ( 42 ) ) ;
15+ let r2 = r. clone ( ) ;
16+ let x = r. get ( ) ;
17+ r2. set ( x + x) ;
18+ assert_eq ! ( r. get( ) , 84 ) ;
19+ }
20+
21+ fn rc_refcell2 ( ) {
22+ let r = Rc :: new ( RefCell :: new ( 42 ) ) ;
23+ let r2 = r. clone ( ) ;
24+ * r. borrow_mut ( ) += 10 ;
25+ let x = r2. borrow ( ) ;
26+ let r3 = r. clone ( ) ;
27+ let y = r3. borrow ( ) ;
28+ assert_eq ! ( ( * x + * y) /2 , 52 ) ;
29+ }
30+
1131fn rc_raw ( ) {
1232 let r = Rc :: new ( 0 ) ;
1333 let r2 = Rc :: into_raw ( r. clone ( ) ) ;
@@ -17,6 +37,14 @@ fn rc_raw() {
1737 assert ! ( Rc :: try_unwrap( r2) . is_ok( ) ) ;
1838}
1939
40+ fn arc ( ) {
41+ fn test ( ) -> Arc < i32 > {
42+ let a = Arc :: new ( 42 ) ;
43+ a
44+ }
45+ assert_eq ! ( * test( ) , 42 ) ;
46+ }
47+
2048// Make sure this Rc doesn't fall apart when touched
2149fn check_unique_rc < T : ?Sized > ( mut r : Rc < T > ) {
2250 let r2 = r. clone ( ) ;
@@ -34,6 +62,9 @@ fn rc_from() {
3462
3563fn main ( ) {
3664 rc_refcell ( ) ;
65+ rc_refcell2 ( ) ;
66+ rc_cell ( ) ;
3767 rc_raw ( ) ;
3868 rc_from ( ) ;
69+ arc ( ) ;
3970}
Original file line number Diff line number Diff line change 11use std:: cell:: RefCell ;
22
3- fn main ( ) {
3+ fn lots_of_funny_borrows ( ) {
44 let c = RefCell :: new ( 42 ) ;
55 {
66 let s1 = c. borrow ( ) ;
@@ -31,3 +31,7 @@ fn main() {
3131 let _y: i32 = * s2;
3232 }
3333}
34+
35+ fn main ( ) {
36+ lots_of_funny_borrows ( ) ;
37+ }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments