Skip to content

Commit c21ee7d

Browse files
committed
Switch outer refcell for inner cells
1 parent efd0b0e commit c21ee7d

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/main.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
use std::cell::{Cell, RefCell};
22

3+
// TODO
4+
// - Nested tracked call
5+
// - Tracked return value from tracked method
6+
// - Tracked methods with arguments
7+
38
fn main() {
49
let mut image = Image::new(20, 40);
510

@@ -48,10 +53,10 @@ fn describe(image: TrackedImage) -> &'static str {
4853
});
4954

5055
let output = output.unwrap_or_else(|| {
51-
let ct = RefCell::new(ImageConstraint::default());
56+
let ct = ImageConstraint::default();
5257
let image = TrackedImage { inner: image.inner, tracker: Some(&ct) };
5358
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)));
5560
hit = false;
5661
output
5762
});
@@ -70,37 +75,37 @@ fn describe(image: TrackedImage) -> &'static str {
7075
#[derive(Copy, Clone)]
7176
struct TrackedImage<'a> {
7277
inner: &'a Image,
73-
tracker: Option<&'a RefCell<ImageConstraint>>,
78+
tracker: Option<&'a ImageConstraint>,
7479
}
7580

7681
impl<'a> TrackedImage<'a> {
7782
fn width(&self) -> u32 {
7883
let output = self.inner.width();
7984
if let Some(tracker) = &self.tracker {
80-
tracker.borrow_mut().width = Some(output);
85+
tracker.width.set(Some(output));
8186
}
8287
output
8388
}
8489

8590
fn height(&self) -> u32 {
8691
let output = self.inner.height();
8792
if let Some(tracker) = &self.tracker {
88-
tracker.borrow_mut().height = Some(output);
93+
tracker.height.set(Some(output));
8994
}
9095
output
9196
}
9297
}
9398

9499
#[derive(Debug, Default)]
95100
struct ImageConstraint {
96-
width: Option<u32>,
97-
height: Option<u32>,
101+
width: Cell<Option<u32>>,
102+
height: Cell<Option<u32>>,
98103
}
99104

100105
impl ImageConstraint {
101106
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())
104109
}
105110
}
106111

0 commit comments

Comments
 (0)