diff --git a/nested/src/singleton/buffer.rs b/nested/src/singleton/buffer.rs index 090bf19..e00ae45 100644 --- a/nested/src/singleton/buffer.rs +++ b/nested/src/singleton/buffer.rs @@ -1,6 +1,7 @@ use { std::{ - sync::{Arc} + sync::{Arc}, + ops::{Deref, DerefMut} }, std::sync::RwLock, crate::{ @@ -14,6 +15,8 @@ use { } }; +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + pub struct SingletonBufferView(Arc>); impl View for SingletonBufferView @@ -30,6 +33,8 @@ where T: Clone + Send + Sync + 'static { } } +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + #[derive(Clone)] pub struct SingletonBuffer where T: Clone + Send + Sync + 'static { @@ -56,6 +61,13 @@ where T: Clone + Send + Sync + 'static { self.value.read().unwrap().clone() } + pub fn get_mut(&self) -> MutableSingletonAccess { + MutableSingletonAccess { + buf: self.clone(), + val: self.get() + } + } + pub fn set(&mut self, new_value: T) { let mut v = self.value.write().unwrap(); *v = new_value; @@ -63,18 +75,36 @@ where T: Clone + Send + Sync + 'static { self.cast.notify(&()); } } -/* -impl SingletonBuffer -where T: Clone + Eq + Send + Sync + 'static { - pub fn set(&mut self, new_value: T) { - let mut v = self.value.write().unwrap(); - if *v != new_value { - *v = new_value; - drop(v); - self.cast.notify(&()); - } + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +pub struct MutableSingletonAccess +where T: Clone + Send + Sync + 'static { + buf: SingletonBuffer, + val: T, +} + +impl Deref for MutableSingletonAccess +where T: Clone + Send + Sync + 'static { + type Target = T; + + fn deref(&self) -> &T { + &self.val } } -*/ -// TODO: impl Deref & DerefMut + +impl DerefMut for MutableSingletonAccess +where T: Clone + Send + Sync + 'static { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.val + } +} + +impl Drop for MutableSingletonAccess +where T: Clone + Send + Sync + 'static { + fn drop(&mut self) { + self.buf.set(self.val.clone()); + } +} +