From 9c21ddbda18426dba91785f77247b10bb2bf556d Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Tue, 12 Jan 2021 23:09:46 +0100 Subject: [PATCH] separate modules for index, sequence & singleton --- src/grid/mod.rs | 2 +- src/grid/offset.rs | 4 +- src/{view/index.rs => index/mod.rs} | 1 + src/sequence/mod.rs | 8 +++ src/singleton/buffer.rs | 61 +++++++++++++++++++++ src/{view/singleton.rs => singleton/mod.rs} | 3 + src/singleton_buffer.rs | 42 -------------- src/terminal/compositor.rs | 2 +- src/terminal/terminal.rs | 4 +- src/view/mod.rs | 11 ---- src/view/sequence.rs | 41 -------------- 11 files changed, 77 insertions(+), 102 deletions(-) rename src/{view/index.rs => index/mod.rs} (99%) create mode 100644 src/sequence/mod.rs create mode 100644 src/singleton/buffer.rs rename src/{view/singleton.rs => singleton/mod.rs} (98%) delete mode 100644 src/singleton_buffer.rs delete mode 100644 src/view/mod.rs delete mode 100644 src/view/sequence.rs diff --git a/src/grid/mod.rs b/src/grid/mod.rs index a961970..75100e6 100644 --- a/src/grid/mod.rs +++ b/src/grid/mod.rs @@ -6,7 +6,7 @@ use { cgmath::{Point2, Vector2}, crate::{ core::View, - view::{IndexView, ImplIndexView} + index::{IndexView, ImplIndexView} } }; diff --git a/src/grid/offset.rs b/src/grid/offset.rs index 749159d..fb70308 100644 --- a/src/grid/offset.rs +++ b/src/grid/offset.rs @@ -12,9 +12,7 @@ use { ObserverBroadcast, InnerViewPort }, - view::{ - index::IndexView - }, + index::{IndexView}, grid::{GridView, GridWindowIterator} } }; diff --git a/src/view/index.rs b/src/index/mod.rs similarity index 99% rename from src/view/index.rs rename to src/index/mod.rs index bed5dac..7aab0c7 100644 --- a/src/view/index.rs +++ b/src/index/mod.rs @@ -1,3 +1,4 @@ + use { std::{ sync::{Arc, RwLock}, diff --git a/src/sequence/mod.rs b/src/sequence/mod.rs new file mode 100644 index 0000000..086b250 --- /dev/null +++ b/src/sequence/mod.rs @@ -0,0 +1,8 @@ +use crate::index::IndexView; + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +pub trait SequenceView = IndexView; + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + diff --git a/src/singleton/buffer.rs b/src/singleton/buffer.rs new file mode 100644 index 0000000..7ce00cc --- /dev/null +++ b/src/singleton/buffer.rs @@ -0,0 +1,61 @@ +use { + std::{ + sync::{Arc, RwLock} + }, + crate::{ + core::{ + Observer, + ObserverBroadcast, + View, + InnerViewPort + }, + singleton::{SingletonView} + } +}; + +pub struct SingletonBuffer +where T: Clone + Eq + Send + Sync + 'static { + value: T, + cast: Arc>>> +} + +impl View for SingletonBuffer +where T: Clone + Eq + Send + Sync + 'static { + type Msg = (); +} + +impl SingletonView for SingletonBuffer +where T: Clone + Eq + Send + Sync + 'static { + type Item = T; + + fn get(&self) -> Self::Item { + self.value.clone() + } +} + +impl SingletonBuffer +where T: Clone + Eq + Send + Sync + 'static { + pub fn new( + value: T, + port: InnerViewPort> + ) -> Arc> { + let buf = Arc::new(RwLock::new( + SingletonBuffer { + value, + cast: port.get_broadcast() + } + )); + port.set_view(Some(buf.clone())); + buf + } + + pub fn set(&mut self, new_value: T) { + if self.value != new_value { + self.value = new_value; + self.cast.notify(&()); + } + } +} + +// TODO: impl Deref & DerefMut + diff --git a/src/view/singleton.rs b/src/singleton/mod.rs similarity index 98% rename from src/view/singleton.rs rename to src/singleton/mod.rs index ca0b6ca..f02ea29 100644 --- a/src/view/singleton.rs +++ b/src/singleton/mod.rs @@ -1,3 +1,6 @@ + +pub mod buffer; + use { std::{ sync::{Arc, RwLock}, diff --git a/src/singleton_buffer.rs b/src/singleton_buffer.rs deleted file mode 100644 index a0ea3c7..0000000 --- a/src/singleton_buffer.rs +++ /dev/null @@ -1,42 +0,0 @@ -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/terminal/compositor.rs b/src/terminal/compositor.rs index 8cb6636..8e92cfe 100644 --- a/src/terminal/compositor.rs +++ b/src/terminal/compositor.rs @@ -8,7 +8,7 @@ use { cgmath::Point2, crate::{ core::{View, ViewPort, InnerViewPort, OuterViewPort, Observer, ObserverExt, ObserverBroadcast}, - view::{ImplIndexView}, + index::{ImplIndexView}, grid::{GridWindowIterator}, terminal::{TerminalAtom, TerminalView} } diff --git a/src/terminal/terminal.rs b/src/terminal/terminal.rs index 7266e8a..c3b763f 100644 --- a/src/terminal/terminal.rs +++ b/src/terminal/terminal.rs @@ -26,9 +26,7 @@ use { set_channel } }, - view::{ - IndexView - }, + index::IndexView, grid::{GridView, GridWindowIterator} }, super::{ diff --git a/src/view/mod.rs b/src/view/mod.rs deleted file mode 100644 index a4c6d6b..0000000 --- a/src/view/mod.rs +++ /dev/null @@ -1,11 +0,0 @@ -pub mod singleton; -pub mod index; -pub mod sequence; - -pub use { - singleton::SingletonView, - index::{IndexView, ImplIndexView}, - sequence::SequenceView, - crate::core::View -}; - diff --git a/src/view/sequence.rs b/src/view/sequence.rs deleted file mode 100644 index 0c00374..0000000 --- a/src/view/sequence.rs +++ /dev/null @@ -1,41 +0,0 @@ -use { - std::{ - sync::{Arc, RwLock}, - ops::{Range, Deref} - }, - super::{IndexView, ImplIndexView}, - crate::core::View -}; - -//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> - -pub trait SequenceView = IndexView; - -//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> -/* -pub trait ImplSequenceView : Send + Sync { - type Item; - - fn get(&self, idx: usize) -> Self::Item; - fn len(&self) -> Option { - None - } -} - -impl ImplIndexView for V { - type Key = usize; - type Value = V::Item; - - fn get(&self, idx: &usize) -> V::Item { - (self as V).get(*idx) - } - - fn range(&self) -> Option> { - if let Some(len) = (self as V).len() { - Some(0 .. len) - } else { - None - } - } -} -*/