diff options
author | natrixaeria <upezu@student.kit.edu> | 2019-12-19 20:16:20 +0100 |
---|---|---|
committer | natrixaeria <upezu@student.kit.edu> | 2019-12-19 20:16:20 +0100 |
commit | 223119654b4835c270ce4215f156e3c6236833b8 (patch) | |
tree | a232cc4b06c1161b92ab2bd05dde97599b35c885 /src/doublebuffer.rs |
Diffstat (limited to 'src/doublebuffer.rs')
-rw-r--r-- | src/doublebuffer.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/doublebuffer.rs b/src/doublebuffer.rs new file mode 100644 index 0000000..563f157 --- /dev/null +++ b/src/doublebuffer.rs @@ -0,0 +1,31 @@ +pub struct DoubleBuffer<T> { + a1: Vec<T>, + a2: Vec<T>, + switch: bool, +} + +impl<T> DoubleBuffer<T> { + pub fn new(a1: Vec<T>, a2: Vec<T>) -> Self { + Self { a1, a2, switch: false } + } + + pub fn switch(&mut self) { + self.switch = !self.switch; + } + + pub fn first(&self) -> &Vec<T> { + if self.switch { &self.a2 } else { &self.a1 } + } + + pub fn first_mut(&mut self) -> &mut Vec<T> { + if self.switch { &mut self.a2 } else { &mut self.a1 } + } + + pub fn second(&self) -> &Vec<T> { + if self.switch { &self.a1 } else { &self.a2 } + } + + pub fn second_mut(&mut self) -> &mut Vec<T> { + if self.switch { &mut self.a1 } else { &mut self.a2 } + } +} |