separate modules for index, sequence & singleton

This commit is contained in:
Michael Sippel 2021-01-12 23:09:46 +01:00
parent c574bf6ad2
commit 9c21ddbda1
Signed by: senvas
GPG key ID: F96CF119C34B64A6
11 changed files with 77 additions and 102 deletions

View file

@ -6,7 +6,7 @@ use {
cgmath::{Point2, Vector2}, cgmath::{Point2, Vector2},
crate::{ crate::{
core::View, core::View,
view::{IndexView, ImplIndexView} index::{IndexView, ImplIndexView}
} }
}; };

View file

@ -12,9 +12,7 @@ use {
ObserverBroadcast, ObserverBroadcast,
InnerViewPort InnerViewPort
}, },
view::{ index::{IndexView},
index::IndexView
},
grid::{GridView, GridWindowIterator} grid::{GridView, GridWindowIterator}
} }
}; };

View file

@ -1,3 +1,4 @@
use { use {
std::{ std::{
sync::{Arc, RwLock}, sync::{Arc, RwLock},

8
src/sequence/mod.rs Normal file
View file

@ -0,0 +1,8 @@
use crate::index::IndexView;
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub trait SequenceView = IndexView<usize>;
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>

61
src/singleton/buffer.rs Normal file
View file

@ -0,0 +1,61 @@
use {
std::{
sync::{Arc, RwLock}
},
crate::{
core::{
Observer,
ObserverBroadcast,
View,
InnerViewPort
},
singleton::{SingletonView}
}
};
pub struct SingletonBuffer<T>
where T: Clone + Eq + Send + Sync + 'static {
value: T,
cast: Arc<RwLock<ObserverBroadcast<dyn SingletonView<Item = T>>>>
}
impl<T> View for SingletonBuffer<T>
where T: Clone + Eq + Send + Sync + 'static {
type Msg = ();
}
impl<T> SingletonView for SingletonBuffer<T>
where T: Clone + Eq + Send + Sync + 'static {
type Item = T;
fn get(&self) -> Self::Item {
self.value.clone()
}
}
impl<T> SingletonBuffer<T>
where T: Clone + Eq + Send + Sync + 'static {
pub fn new(
value: T,
port: InnerViewPort<dyn SingletonView<Item = T>>
) -> Arc<RwLock<Self>> {
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

View file

@ -1,3 +1,6 @@
pub mod buffer;
use { use {
std::{ std::{
sync::{Arc, RwLock}, sync::{Arc, RwLock},

View file

@ -1,42 +0,0 @@
use {
std::{
sync::{Arc, RwLock}
},
crate::{
view::Observer,
port::InnerViewPort
}
};
pub struct SingletonBuffer<T: Clone + Eq + Send + Sync + 'static> {
data: Arc<RwLock<Option<T>>>,
port: InnerViewPort<(), T>
}
impl<T: Clone + Eq + Send + Sync + 'static> SingletonBuffer<T> {
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(());
}
}
}

View file

@ -8,7 +8,7 @@ use {
cgmath::Point2, cgmath::Point2,
crate::{ crate::{
core::{View, ViewPort, InnerViewPort, OuterViewPort, Observer, ObserverExt, ObserverBroadcast}, core::{View, ViewPort, InnerViewPort, OuterViewPort, Observer, ObserverExt, ObserverBroadcast},
view::{ImplIndexView}, index::{ImplIndexView},
grid::{GridWindowIterator}, grid::{GridWindowIterator},
terminal::{TerminalAtom, TerminalView} terminal::{TerminalAtom, TerminalView}
} }

View file

@ -26,9 +26,7 @@ use {
set_channel set_channel
} }
}, },
view::{ index::IndexView,
IndexView
},
grid::{GridView, GridWindowIterator} grid::{GridView, GridWindowIterator}
}, },
super::{ super::{

View file

@ -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
};

View file

@ -1,41 +0,0 @@
use {
std::{
sync::{Arc, RwLock},
ops::{Range, Deref}
},
super::{IndexView, ImplIndexView},
crate::core::View
};
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub trait SequenceView = IndexView<usize>;
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
/*
pub trait ImplSequenceView : Send + Sync {
type Item;
fn get(&self, idx: usize) -> Self::Item;
fn len(&self) -> Option<usize> {
None
}
}
impl<V: ImplSequenceView> 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<Range<usize>> {
if let Some(len) = (self as V).len() {
Some(0 .. len)
} else {
None
}
}
}
*/