From c8a28efc8cb64a9e2045a19d68a1fb4a9f935e03 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Thu, 17 Feb 2022 10:55:22 -0500 Subject: [PATCH] cleanup Cache struct --- src/misc.rs | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/misc.rs b/src/misc.rs index 809d7de..f520cc7 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -118,8 +118,7 @@ impl ChartOutput { } pub struct Cache { - backing_data: Option, - valid: bool, + backing_data: Option } impl Cache { @@ -127,25 +126,19 @@ impl Cache { #[inline] pub fn new(backing_data: T) -> Self { Self { - backing_data: Some(backing_data), - valid: true, + backing_data: Some(backing_data) } } #[inline] pub fn new_empty() -> Self { Self { - backing_data: None, - valid: false, + backing_data: None } } #[inline] pub fn get(&self) -> &T { - if !self.valid { - panic!("self.valid is false, but get() method was called!") - } - match &self.backing_data { Some(x) => x, None => panic!("self.backing_data is None"), @@ -154,18 +147,21 @@ impl Cache { #[inline] pub fn set(&mut self, data: T) { - self.valid = true; self.backing_data = Some(data); } #[inline] pub fn invalidate(&mut self) { - self.valid = false; self.backing_data = None; } #[inline] - pub fn is_valid(&self) -> bool { self.valid } + pub fn is_valid(&self) -> bool { + match &self.backing_data { + Some(_) => true, + None => false + } + } } // Tests to make sure my cursed function works as intended