1
1
use std:: cell:: { Cell , RefCell } ;
2
2
3
+ // TODO
4
+ // - Nested tracked call
5
+ // - Tracked return value from tracked method
6
+ // - Tracked methods with arguments
7
+
3
8
fn main ( ) {
4
9
let mut image = Image :: new ( 20 , 40 ) ;
5
10
@@ -48,10 +53,10 @@ fn describe(image: TrackedImage) -> &'static str {
48
53
} ) ;
49
54
50
55
let output = output. unwrap_or_else ( || {
51
- let ct = RefCell :: new ( ImageConstraint :: default ( ) ) ;
56
+ let ct = ImageConstraint :: default ( ) ;
52
57
let image = TrackedImage { inner : image. inner , tracker : Some ( & ct) } ;
53
58
let output = inner ( image) ;
54
- CACHE . with ( |cache| cache. borrow_mut ( ) . push ( ( ct. into_inner ( ) , output) ) ) ;
59
+ CACHE . with ( |cache| cache. borrow_mut ( ) . push ( ( ct, output) ) ) ;
55
60
hit = false ;
56
61
output
57
62
} ) ;
@@ -70,37 +75,37 @@ fn describe(image: TrackedImage) -> &'static str {
70
75
#[ derive( Copy , Clone ) ]
71
76
struct TrackedImage < ' a > {
72
77
inner : & ' a Image ,
73
- tracker : Option < & ' a RefCell < ImageConstraint > > ,
78
+ tracker : Option < & ' a ImageConstraint > ,
74
79
}
75
80
76
81
impl < ' a > TrackedImage < ' a > {
77
82
fn width ( & self ) -> u32 {
78
83
let output = self . inner . width ( ) ;
79
84
if let Some ( tracker) = & self . tracker {
80
- tracker. borrow_mut ( ) . width = Some ( output) ;
85
+ tracker. width . set ( Some ( output) ) ;
81
86
}
82
87
output
83
88
}
84
89
85
90
fn height ( & self ) -> u32 {
86
91
let output = self . inner . height ( ) ;
87
92
if let Some ( tracker) = & self . tracker {
88
- tracker. borrow_mut ( ) . height = Some ( output) ;
93
+ tracker. height . set ( Some ( output) ) ;
89
94
}
90
95
output
91
96
}
92
97
}
93
98
94
99
#[ derive( Debug , Default ) ]
95
100
struct ImageConstraint {
96
- width : Option < u32 > ,
97
- height : Option < u32 > ,
101
+ width : Cell < Option < u32 > > ,
102
+ height : Cell < Option < u32 > > ,
98
103
}
99
104
100
105
impl ImageConstraint {
101
106
fn valid ( & self , image : & Image ) -> bool {
102
- self . width . map_or ( true , |v| v == image. width ( ) )
103
- && self . height . map_or ( true , |v| v == image. height ( ) )
107
+ self . width . get ( ) . map_or ( true , |v| v == image. width ( ) )
108
+ && self . height . get ( ) . map_or ( true , |v| v == image. height ( ) )
104
109
}
105
110
}
106
111
0 commit comments