improve vec buffer
add get_mut() and refactor wrapper methods
This commit is contained in:
parent
6aef446134
commit
4229278369
3 changed files with 65 additions and 15 deletions
|
@ -14,7 +14,6 @@ use {
|
||||||
View Port
|
View Port
|
||||||
<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
||||||
\*/
|
\*/
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct ViewPort<V: View + ?Sized> {
|
pub struct ViewPort<V: View + ?Sized> {
|
||||||
view: Arc<RwLock<Option<Arc<V>>>>,
|
view: Arc<RwLock<Option<Arc<V>>>>,
|
||||||
observers: Arc<RwLock<ObserverBroadcast<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)]
|
#[derive(Clone)]
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use {
|
use {
|
||||||
std::{
|
std::{
|
||||||
sync::{Arc, RwLock}
|
sync::{Arc, RwLock},
|
||||||
|
ops::{Deref, DerefMut}
|
||||||
},
|
},
|
||||||
crate::{
|
crate::{
|
||||||
core::{View, Observer, ObserverExt, ObserverBroadcast, ViewPort, InnerViewPort, OuterViewPort},
|
core::{View, Observer, ObserverExt, ObserverBroadcast, ViewPort, InnerViewPort, OuterViewPort},
|
||||||
|
@ -122,6 +123,7 @@ where T: Clone + Send + Sync + 'static {
|
||||||
|
|
||||||
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct VecBuffer<T>
|
pub struct VecBuffer<T>
|
||||||
where T: Clone + Send + Sync + 'static {
|
where T: Clone + Send + Sync + 'static {
|
||||||
data: Arc<RwLock<Vec<T>>>,
|
data: Arc<RwLock<Vec<T>>>,
|
||||||
|
@ -144,12 +146,15 @@ where T: Clone + Send + Sync + 'static {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply_diff(&mut self, diff: VecDiff<T>) {
|
pub fn apply_diff(&mut self, diff: VecDiff<T>) {
|
||||||
match diff {
|
let mut data = self.data.write().unwrap();
|
||||||
VecDiff::Push(val) => self.push(val),
|
match &diff {
|
||||||
VecDiff::Remove(idx) => self.remove(idx),
|
VecDiff::Push(val) => { data.push(val.clone()); },
|
||||||
VecDiff::Insert{ idx, val } => self.insert(idx, val),
|
VecDiff::Remove(idx) => { data.remove(*idx); },
|
||||||
VecDiff::Update{ idx, val } => self.update(idx, val)
|
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 {
|
pub fn len(&self) -> usize {
|
||||||
|
@ -161,23 +166,59 @@ where T: Clone + Send + Sync + 'static {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push(&mut self, val: T) {
|
pub fn push(&mut self, val: T) {
|
||||||
self.data.write().unwrap().push(val.clone());
|
self.apply_diff(VecDiff::Push(val));
|
||||||
self.cast.notify(&VecDiff::Push(val));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn remove(&mut self, idx: usize) {
|
pub fn remove(&mut self, idx: usize) {
|
||||||
self.data.write().unwrap().remove(idx);
|
self.apply_diff(VecDiff::Remove(idx));
|
||||||
self.cast.notify(&VecDiff::Remove(idx));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(&mut self, idx: usize, val: T) {
|
pub fn insert(&mut self, idx: usize, val: T) {
|
||||||
self.data.write().unwrap().insert(idx, val.clone());
|
self.apply_diff(VecDiff::Insert{ idx, val });
|
||||||
self.cast.notify(&VecDiff::Insert{ idx, val });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, idx: usize, val: T) {
|
pub fn update(&mut self, idx: usize, val: T) {
|
||||||
self.data.write().unwrap()[idx] = val.clone();
|
self.apply_diff(VecDiff::Update{ idx, val });
|
||||||
self.cast.notify(&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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,7 @@ where T: Clone + Eq + Send + Sync + 'static {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct SingletonBuffer<T>
|
pub struct SingletonBuffer<T>
|
||||||
where T: Clone + Eq + Send + Sync + 'static {
|
where T: Clone + Eq + Send + Sync + 'static {
|
||||||
value: Arc<RwLock<T>>,
|
value: Arc<RwLock<T>>,
|
||||||
|
|
Loading…
Reference in a new issue