summaryrefslogtreecommitdiff
path: root/src/doublebuffer.rs
diff options
context:
space:
mode:
authornatrixaeria <upezu@student.kit.edu>2019-12-19 20:16:20 +0100
committernatrixaeria <upezu@student.kit.edu>2019-12-19 20:16:20 +0100
commit223119654b4835c270ce4215f156e3c6236833b8 (patch)
treea232cc4b06c1161b92ab2bd05dde97599b35c885 /src/doublebuffer.rs
Initial commitHEADmaster
Diffstat (limited to 'src/doublebuffer.rs')
-rw-r--r--src/doublebuffer.rs31
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 }
+ }
+}