Compare commits

..

No commits in common. "f0ac6e6eb3e22c37fe56c84c503b773720c5e94a" and "df50cf621b4cbab40094de2a438cdd45f7684c08" have entirely different histories.

3 changed files with 4 additions and 141 deletions

View file

@ -1,9 +1,6 @@
use {
crate::{
view::{
InnerViewPort, OuterViewPort, View, Observer, ViewPort,
list::*
}
view::{InnerViewPort, OuterViewPort, View, ViewPort},
},
std::sync::RwLock,
std::{
@ -34,42 +31,6 @@ where
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub struct VecBufferTarget<T>
where T: Clone + Send + Sync + 'static
{
buffer: VecBuffer<T>,
src_view: Option<Arc<dyn ListView<T>>>
}
impl<T> Observer< dyn ListView<T> > for VecBufferTarget<T>
where T: Clone + Send + Sync + 'static
{
fn notify(&mut self, msg: &ListDiff<T>) {
self.buffer.apply_diff(
match msg.clone() {
ListDiff::Clear => VecDiff::Clear,
ListDiff::Remove(idx) => VecDiff::Remove(idx),
ListDiff::Insert{ idx, val } => VecDiff::Insert{ idx, val },
ListDiff::Update{ idx, val } => VecDiff::Update{ idx, val }
}
);
}
fn reset(&mut self, src_view: Option<Arc<dyn ListView<T>>>) {
self.src_view = src_view;
// copy all elements of new view
if let Some(v) = self.src_view.as_ref() {
self.buffer.clear();
for x in v.iter() {
self.buffer.push(x.clone());
}
}
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
#[derive(Clone)]
pub struct VecBuffer<T>
where
@ -97,20 +58,6 @@ where
}
}
pub fn attach_to(&self, port: OuterViewPort< dyn ListView<T> >) -> Arc<RwLock<VecBufferTarget<T>>> {
self.port.0.add_update_hook(Arc::new(port.0.clone()));
let target = Arc::new(RwLock::new(
VecBufferTarget {
buffer: self.clone(),
src_view: None
}
));
port.add_observer(target.clone());
target
}
pub fn with_data(data: Vec<T>) -> Self {
VecBuffer::with_data_port(data, ViewPort::new().into_inner())
}
@ -186,10 +133,6 @@ where
val: self.get(idx),
}
}
pub fn into_inner(self) -> Arc<RwLock<Vec<T>>> {
self.data
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
@ -236,13 +179,7 @@ where
#[cfg(test)]
mod tests {
use crate::{
buffer::vec::*,
view::{
port::UpdateTask,
list::ListView,
}
};
use crate::buffer::vec::*;
#[test]
fn vec_buffer1() {
@ -252,37 +189,5 @@ mod tests {
buffer.push('b');
buffer.push('c');
}
#[test]
fn list_to_vec() {
let mut buf = VecBuffer::<char>::new();
let list_view = buf.get_port().to_list();
let mut buf2 = VecBuffer::new();
let keepalive = buf2.attach_to( list_view.clone() );
assert_eq!(buf2.len(), 0);
buf.push('a');
buf2.get_port().0.update();
assert_eq!(buf2.len(), 1);
assert_eq!(buf2.get(0), 'a');
buf.push('b');
list_view.0.update();
assert_eq!(buf2.len(), 2);
assert_eq!(buf2.get(0), 'a');
assert_eq!(buf2.get(1), 'b');
buf.push('c');
buf.remove(0);
list_view.0.update();
assert_eq!(buf2.len(), 2);
assert_eq!(buf2.get(0), 'b');
assert_eq!(buf2.get(1), 'c');
}
}

View file

@ -1,9 +1,7 @@
use {
crate::{
view::{
View, ViewPort,
InnerViewPort, OuterViewPort,
Observer, ObserverBroadcast, ObserverExt,
InnerViewPort, Observer, ObserverBroadcast, ObserverExt, OuterViewPort, View, ViewPort,
list::{ListView, ListDiff},
},
buffer::vec::VecDiff,
@ -71,7 +69,7 @@ where
}
VecDiff::Push(val) => {
self.cast.notify(&ListDiff::Insert{
idx: self.cur_len,
idx: self.cur_len + 1,
val: val.clone()
});
self.cur_len += 1;

View file

@ -21,46 +21,6 @@ where Item: Clone + Send + Sync + 'static
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub trait ListViewExt<T>: ListView<T>
where T: Clone + Send + Sync + 'static
{
fn iter<'a>(&'a self) -> ListViewIter<'a, T, Self> {
ListViewIter { _phantom: std::marker::PhantomData, view: self, cur: 0 }
}
}
impl<T, V: ListView<T> + ?Sized> ListViewExt<T> for V
where T: Clone + Send + Sync + 'static
{}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub struct ListViewIter<'a, T, V>
where
T: Clone + Send + Sync + 'static,
V: ListView<T> + ?Sized,
{
_phantom: std::marker::PhantomData<T>,
view: &'a V,
cur: usize,
}
impl<'a, T, V> Iterator for ListViewIter<'a, T, V>
where
T: Clone + Send + Sync + 'static,
V: ListView<T> + ?Sized,
{
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
let i = self.cur;
self.cur += 1;
self.view.get(&i)
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
use std::sync::RwLock;
use std::{ops::Deref, sync::Arc};