From 4229278369aac3bb742814ef858b53555315cc6a Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Tue, 19 Jan 2021 23:13:39 +0100 Subject: [PATCH] improve vec buffer add get_mut() and refactor wrapper methods --- src/core/port.rs | 10 +++++- src/sequence/vec_buffer.rs | 69 ++++++++++++++++++++++++++++++-------- src/singleton/buffer.rs | 1 + 3 files changed, 65 insertions(+), 15 deletions(-) diff --git a/src/core/port.rs b/src/core/port.rs index fc9da84..5e4b428 100644 --- a/src/core/port.rs +++ b/src/core/port.rs @@ -14,7 +14,6 @@ use { View Port <<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> \*/ -#[derive(Clone)] pub struct ViewPort { view: Arc>>>, observers: Arc>> @@ -61,6 +60,15 @@ impl ViewPort { } } +impl Clone for ViewPort { + fn clone(&self) -> Self { + ViewPort { + view: self.view.clone(), + observers: self.observers.clone() + } + } +} + //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> #[derive(Clone)] diff --git a/src/sequence/vec_buffer.rs b/src/sequence/vec_buffer.rs index 70ff10a..f2e32ca 100644 --- a/src/sequence/vec_buffer.rs +++ b/src/sequence/vec_buffer.rs @@ -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 where T: Clone + Send + Sync + 'static { data: Arc>>, @@ -144,12 +146,15 @@ where T: Clone + Send + Sync + 'static { } pub fn apply_diff(&mut self, diff: VecDiff) { - 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 { + MutableVecAccess { + buf: self.clone(), + idx, + val: self.get(idx) + } + } +} + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +pub struct MutableVecAccess +where T: Clone + Send + Sync + 'static { + buf: VecBuffer, + idx: usize, + val: T, +} + +impl Deref for MutableVecAccess +where T: Clone + Send + Sync + 'static { + type Target = T; + + fn deref(&self) -> &T { + &self.val + } +} + +impl DerefMut for MutableVecAccess +where T: Clone + Send + Sync + 'static { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.val + } +} + +impl Drop for MutableVecAccess +where T: Clone + Send + Sync + 'static { + fn drop(&mut self) { + self.buf.update(self.idx, self.val.clone()); } } diff --git a/src/singleton/buffer.rs b/src/singleton/buffer.rs index 39db573..4cac1aa 100644 --- a/src/singleton/buffer.rs +++ b/src/singleton/buffer.rs @@ -29,6 +29,7 @@ where T: Clone + Eq + Send + Sync + 'static { } } +#[derive(Clone)] pub struct SingletonBuffer where T: Clone + Eq + Send + Sync + 'static { value: Arc>,