handle Cache in 'Cache' struct
This commit is contained in:
54
src/misc.rs
54
src/misc.rs
@@ -11,6 +11,7 @@ pub struct Point {
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl Point {
|
||||
#[inline]
|
||||
pub fn new(x: f32, y: f32) -> Self { Self { x, y } }
|
||||
}
|
||||
|
||||
@@ -28,3 +29,56 @@ impl Chart {
|
||||
(self.convert)((x, y)).map(|(x, y)| Point::new(x, y))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Cache<T> {
|
||||
backing_data: Option<T>,
|
||||
valid: bool,
|
||||
}
|
||||
|
||||
impl<T> Cache<T> {
|
||||
#[allow(dead_code)]
|
||||
#[inline]
|
||||
pub fn new(backing_data: T) -> Self {
|
||||
Self {
|
||||
backing_data: Some(backing_data),
|
||||
valid: true,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn new_empty() -> Self {
|
||||
Self {
|
||||
backing_data: None,
|
||||
valid: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[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"),
|
||||
}
|
||||
}
|
||||
|
||||
#[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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user