summaryrefslogtreecommitdiff
path: root/src/doublebuffer.rs
diff options
context:
space:
mode:
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 }
+ }
+}