make SequenceView a separate trait which can be converted into an IndexView with a projection

This commit is contained in:
Michael Sippel 2021-01-16 13:59:48 +01:00
parent 9d6b1aee74
commit 589052977c
Signed by: senvas
GPG key ID: F96CF119C34B64A6
2 changed files with 136 additions and 2 deletions

View file

@ -1,8 +1,52 @@
use crate::index::IndexView;
pub mod seq2idx;
pub mod vec_buffer;
pub use {
seq2idx::{Sequence2Index},
vec_buffer::{VecBuffer, VecSequence}
};
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub trait SequenceView = IndexView<usize>;
use crate::core::View;
pub trait SequenceView : View<Msg = usize> {
type Item;
fn get(&self, idx: usize) -> Self::Item;
fn len(&self) -> Option<usize>;
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
use std::{
sync::{Arc, RwLock},
ops::{Deref}
};
impl<V: SequenceView> SequenceView for RwLock<V> {
type Item = V::Item;
fn get(&self, idx: usize) -> Self::Item {
self.read().unwrap().get(idx)
}
fn len(&self) -> Option<usize> {
self.read().unwrap().len()
}
}
impl<V: SequenceView> SequenceView for Arc<V> {
type Item = V::Item;
fn get(&self, idx: usize) -> Self::Item {
self.deref().get(idx)
}
fn len(&self) -> Option<usize> {
self.deref().len()
}
}

90
src/sequence/seq2idx.rs Normal file
View file

@ -0,0 +1,90 @@
use {
std::{
sync::{Arc, RwLock},
boxed::Box
},
crate::{
core::{
View, Observer, ObserverExt, ObserverBroadcast,
ViewPort, InnerViewPort, OuterViewPort
},
sequence::SequenceView,
index::IndexView
}
};
/// Transforms a SequenceView into IndexView<usize>
pub struct Sequence2Index<SrcView>
where SrcView: SequenceView + ?Sized + 'static {
src_view: Option<Arc<SrcView>>,
cast: Arc<RwLock<ObserverBroadcast<dyn IndexView<usize, Item = Option<SrcView::Item>>>>>
}
impl<SrcView> Sequence2Index<SrcView>
where SrcView: SequenceView + ?Sized + 'static {
pub fn new(
port: InnerViewPort<dyn IndexView<usize, Item = Option<SrcView::Item>>>
) -> Arc<RwLock<Self>> {
let s2i = Arc::new(RwLock::new(
Sequence2Index {
src_view: None,
cast: port.get_broadcast()
}
));
port.set_view(Some(s2i.clone()));
s2i
}
}
impl<Item: 'static> OuterViewPort<dyn SequenceView<Item = Item>> {
pub fn to_index(&self) -> OuterViewPort<dyn IndexView<usize, Item = Option<Item>>> {
let port = ViewPort::new();
self.add_observer(Sequence2Index::new(port.inner()));
port.into_outer()
}
}
impl<SrcView> View for Sequence2Index<SrcView>
where SrcView: SequenceView + ?Sized + 'static {
type Msg = usize;
}
impl<SrcView> IndexView<usize> for Sequence2Index<SrcView>
where SrcView: SequenceView + ?Sized + 'static {
type Item = Option<SrcView::Item>;
fn get(&self, key: &usize) -> Self::Item {
if let Some(v) = self.src_view.as_ref() {
if *key < v.len().unwrap_or(usize::MAX) {
return Some(v.get(*key));
}
}
None
}
fn area(&self) -> Option<Vec<usize>> {
if let Some(v) = self.src_view.as_ref() {
if let Some(len) = v.len() {
return Some((0 .. len).collect());
}
}
None
}
}
impl<SrcView> Observer<SrcView> for Sequence2Index<SrcView>
where SrcView: SequenceView + ?Sized + 'static {
fn reset(&mut self, view: Option<Arc<SrcView>>) {
let old_area = self.area();
self.src_view = view;
let new_area = self.area();
if let Some(area) = old_area { self.cast.notify_each(area); }
if let Some(area) = new_area { self.cast.notify_each(area); }
}
fn notify(&self, msg: &usize) {
self.cast.notify(msg);
}
}