From 4988db36e82346eeb2714de5c19623560635cb35 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Tue, 8 Dec 2020 15:58:23 +0100 Subject: [PATCH] move VecBuffer & SingletonBuffer into separate files --- src/main.rs | 72 +++-------------------------------------- src/singleton_buffer.rs | 42 ++++++++++++++++++++++++ src/vec_buffer.rs | 43 ++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 67 deletions(-) create mode 100644 src/singleton_buffer.rs create mode 100644 src/vec_buffer.rs diff --git a/src/main.rs b/src/main.rs index 2f46611..4593455 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,8 @@ pub mod view; pub mod port; pub mod channel; +pub mod singleton_buffer; +pub mod vec_buffer; use { async_std::{ @@ -16,76 +18,12 @@ use { cgmath::{Vector2}, crate::{ view::{View, Observer}, - port::{InnerViewPort, OuterViewPort} + port::{InnerViewPort, OuterViewPort}, + singleton_buffer::SingletonBuffer, + vec_buffer::VecBuffer } }; -struct SingletonBuffer { - data: Arc>>, - port: InnerViewPort<(), T> -} - -impl SingletonBuffer { - fn new( - port: InnerViewPort<(), T> - ) -> Self { - let data = Arc::new(RwLock::new(None)); - - port.set_view_fn({ - let data = data.clone(); - move |_| data.read().unwrap().clone() - }); - - SingletonBuffer { - data, - port - } - } - - fn update(&mut self, new_value: T) { - let mut data = self.data.write().unwrap(); - if *data != Some(new_value.clone()) { - *data = Some(new_value); - drop(data); - self.port.notify(()); - } - } -} - - - -impl View for Vec { - type Key = usize; - type Value = T; - - fn view(&self, key: usize) -> Option { - self.get(key).cloned() - } -} - - -struct VecBuffer { - data: Arc>>, - port: InnerViewPort -} - -impl VecBuffer { - fn new(port: InnerViewPort) -> Self { - let data = Arc::new(RwLock::new(Vec::new())); - port.set_view(data.clone()); - VecBuffer { data, port } - } - - fn push(&mut self, val: T) { - self.port.notify({ - let mut d = self.data.write().unwrap(); - let len = d.len(); - d.push(val); - len - }); - } -} - #[async_std::main] async fn main() { let digits = port::ViewPort::new(); diff --git a/src/singleton_buffer.rs b/src/singleton_buffer.rs new file mode 100644 index 0000000..a0ea3c7 --- /dev/null +++ b/src/singleton_buffer.rs @@ -0,0 +1,42 @@ +use { + std::{ + sync::{Arc, RwLock} + }, + crate::{ + view::Observer, + port::InnerViewPort + } +}; + +pub struct SingletonBuffer { + data: Arc>>, + port: InnerViewPort<(), T> +} + +impl SingletonBuffer { + pub fn new( + port: InnerViewPort<(), T> + ) -> Self { + let data = Arc::new(RwLock::new(None)); + + port.set_view_fn({ + let data = data.clone(); + move |_| data.read().unwrap().clone() + }); + + SingletonBuffer { + data, + port + } + } + + pub fn update(&mut self, new_value: T) { + let mut data = self.data.write().unwrap(); + if *data != Some(new_value.clone()) { + *data = Some(new_value); + drop(data); + self.port.notify(()); + } + } +} + diff --git a/src/vec_buffer.rs b/src/vec_buffer.rs new file mode 100644 index 0000000..b3541f6 --- /dev/null +++ b/src/vec_buffer.rs @@ -0,0 +1,43 @@ +use { + std::{ + sync::{Arc, RwLock} + }, + crate::{ + view::{View, Observer}, + port::{InnerViewPort} + } +}; + +impl View for Vec { + type Key = usize; + type Value = T; + + fn view(&self, key: usize) -> Option { + self.get(key).cloned() + } +} + +pub struct VecBuffer { + data: Arc>>, + port: InnerViewPort +} + +impl VecBuffer { + pub fn new(port: InnerViewPort) -> Self { + let data = Arc::new(RwLock::new(Vec::new())); + port.set_view(data.clone()); + VecBuffer { data, port } + } + + pub fn push(&mut self, val: T) { + self.port.notify({ + let mut d = self.data.write().unwrap(); + let len = d.len(); + d.push(val); + len + }); + } + + // TODO: add functions +} +