improve vec buffer

add get_mut() and refactor wrapper methods
This commit is contained in:
Michael Sippel 2021-01-19 23:13:39 +01:00
parent 6aef446134
commit 4229278369
Signed by: senvas
GPG key ID: F96CF119C34B64A6
3 changed files with 65 additions and 15 deletions

View file

@ -14,7 +14,6 @@ use {
View Port
<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
\*/
#[derive(Clone)]
pub struct ViewPort<V: View + ?Sized> {
view: Arc<RwLock<Option<Arc<V>>>>,
observers: Arc<RwLock<ObserverBroadcast<V>>>
@ -61,6 +60,15 @@ impl<V: View + ?Sized> ViewPort<V> {
}
}
impl<V: View + ?Sized> Clone for ViewPort<V> {
fn clone(&self) -> Self {
ViewPort {
view: self.view.clone(),
observers: self.observers.clone()
}
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
#[derive(Clone)]

View file

@ -1,6 +1,7 @@
use {
std::{
sync::{Arc, RwLock}
sync::{Arc, RwLock},
ops::{Deref, DerefMut}
},
crate::{
core::{View, Observer, ObserverExt, ObserverBroadcast, ViewPort, InnerViewPort, OuterViewPort},
@ -122,6 +123,7 @@ where T: Clone + Send + Sync + 'static {
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
#[derive(Clone)]
pub struct VecBuffer<T>
where T: Clone + Send + Sync + 'static {
data: Arc<RwLock<Vec<T>>>,
@ -144,12 +146,15 @@ where T: Clone + Send + Sync + 'static {
}
pub fn apply_diff(&mut self, diff: VecDiff<T>) {
match diff {
VecDiff::Push(val) => self.push(val),
VecDiff::Remove(idx) => self.remove(idx),
VecDiff::Insert{ idx, val } => self.insert(idx, val),
VecDiff::Update{ idx, val } => self.update(idx, val)
let mut data = self.data.write().unwrap();
match &diff {
VecDiff::Push(val) => { data.push(val.clone()); },
VecDiff::Remove(idx) => { data.remove(*idx); },
VecDiff::Insert{ idx, val } => { data.insert(*idx, val.clone()); },
VecDiff::Update{ idx, val } => { data[*idx] = val.clone(); }
}
drop(data);
self.cast.notify(&diff);
}
pub fn len(&self) -> usize {
@ -161,23 +166,59 @@ where T: Clone + Send + Sync + 'static {
}
pub fn push(&mut self, val: T) {
self.data.write().unwrap().push(val.clone());
self.cast.notify(&VecDiff::Push(val));
self.apply_diff(VecDiff::Push(val));
}
pub fn remove(&mut self, idx: usize) {
self.data.write().unwrap().remove(idx);
self.cast.notify(&VecDiff::Remove(idx));
self.apply_diff(VecDiff::Remove(idx));
}
pub fn insert(&mut self, idx: usize, val: T) {
self.data.write().unwrap().insert(idx, val.clone());
self.cast.notify(&VecDiff::Insert{ idx, val });
self.apply_diff(VecDiff::Insert{ idx, val });
}
pub fn update(&mut self, idx: usize, val: T) {
self.data.write().unwrap()[idx] = val.clone();
self.cast.notify(&VecDiff::Update{ idx, val });
self.apply_diff(VecDiff::Update{ idx, val });
}
pub fn get_mut(&mut self, idx: usize) -> MutableVecAccess<T> {
MutableVecAccess {
buf: self.clone(),
idx,
val: self.get(idx)
}
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub struct MutableVecAccess<T>
where T: Clone + Send + Sync + 'static {
buf: VecBuffer<T>,
idx: usize,
val: T,
}
impl<T> Deref for MutableVecAccess<T>
where T: Clone + Send + Sync + 'static {
type Target = T;
fn deref(&self) -> &T {
&self.val
}
}
impl<T> DerefMut for MutableVecAccess<T>
where T: Clone + Send + Sync + 'static {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.val
}
}
impl<T> Drop for MutableVecAccess<T>
where T: Clone + Send + Sync + 'static {
fn drop(&mut self) {
self.buf.update(self.idx, self.val.clone());
}
}

View file

@ -29,6 +29,7 @@ where T: Clone + Eq + Send + Sync + 'static {
}
}
#[derive(Clone)]
pub struct SingletonBuffer<T>
where T: Clone + Eq + Send + Sync + 'static {
value: Arc<RwLock<T>>,