cleanup Cache struct
This commit is contained in:
parent
0154ff3933
commit
c8a28efc8c
22
src/misc.rs
22
src/misc.rs
@ -118,8 +118,7 @@ impl ChartOutput {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct Cache<T> {
|
pub struct Cache<T> {
|
||||||
backing_data: Option<T>,
|
backing_data: Option<T>
|
||||||
valid: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Cache<T> {
|
impl<T> Cache<T> {
|
||||||
@ -127,25 +126,19 @@ impl<T> Cache<T> {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn new(backing_data: T) -> Self {
|
pub fn new(backing_data: T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
backing_data: Some(backing_data),
|
backing_data: Some(backing_data)
|
||||||
valid: true,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new_empty() -> Self {
|
pub fn new_empty() -> Self {
|
||||||
Self {
|
Self {
|
||||||
backing_data: None,
|
backing_data: None
|
||||||
valid: false,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get(&self) -> &T {
|
pub fn get(&self) -> &T {
|
||||||
if !self.valid {
|
|
||||||
panic!("self.valid is false, but get() method was called!")
|
|
||||||
}
|
|
||||||
|
|
||||||
match &self.backing_data {
|
match &self.backing_data {
|
||||||
Some(x) => x,
|
Some(x) => x,
|
||||||
None => panic!("self.backing_data is None"),
|
None => panic!("self.backing_data is None"),
|
||||||
@ -154,18 +147,21 @@ impl<T> Cache<T> {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn set(&mut self, data: T) {
|
pub fn set(&mut self, data: T) {
|
||||||
self.valid = true;
|
|
||||||
self.backing_data = Some(data);
|
self.backing_data = Some(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn invalidate(&mut self) {
|
pub fn invalidate(&mut self) {
|
||||||
self.valid = false;
|
|
||||||
self.backing_data = None;
|
self.backing_data = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[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
|
// Tests to make sure my cursed function works as intended
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user