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 } ;
2
2
use std:: rc:: Rc ;
3
+ use std:: sync:: Arc ;
3
4
4
5
fn rc_refcell ( ) {
5
6
let r = Rc :: new ( RefCell :: new ( 42 ) ) ;
7
+ let r2 = r. clone ( ) ;
6
8
* r. borrow_mut ( ) += 10 ;
7
- let x = * r . borrow ( ) ;
9
+ let x = * r2 . borrow ( ) ;
8
10
assert_eq ! ( x, 52 ) ;
9
11
}
10
12
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
+
11
31
fn rc_raw ( ) {
12
32
let r = Rc :: new ( 0 ) ;
13
33
let r2 = Rc :: into_raw ( r. clone ( ) ) ;
@@ -17,6 +37,14 @@ fn rc_raw() {
17
37
assert ! ( Rc :: try_unwrap( r2) . is_ok( ) ) ;
18
38
}
19
39
40
+ fn arc ( ) {
41
+ fn test ( ) -> Arc < i32 > {
42
+ let a = Arc :: new ( 42 ) ;
43
+ a
44
+ }
45
+ assert_eq ! ( * test( ) , 42 ) ;
46
+ }
47
+
20
48
// Make sure this Rc doesn't fall apart when touched
21
49
fn check_unique_rc < T : ?Sized > ( mut r : Rc < T > ) {
22
50
let r2 = r. clone ( ) ;
@@ -34,6 +62,9 @@ fn rc_from() {
34
62
35
63
fn main ( ) {
36
64
rc_refcell ( ) ;
65
+ rc_refcell2 ( ) ;
66
+ rc_cell ( ) ;
37
67
rc_raw ( ) ;
38
68
rc_from ( ) ;
69
+ arc ( ) ;
39
70
}
Original file line number Diff line number Diff line change 1
1
use std:: cell:: RefCell ;
2
2
3
- fn main ( ) {
3
+ fn lots_of_funny_borrows ( ) {
4
4
let c = RefCell :: new ( 42 ) ;
5
5
{
6
6
let s1 = c. borrow ( ) ;
@@ -31,3 +31,7 @@ fn main() {
31
31
let _y: i32 = * s2;
32
32
}
33
33
}
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