From 4ab20be1c57418e1307b256cc2199486bc5060fa Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Thu, 18 Nov 2021 14:43:10 +0100 Subject: [PATCH] SingletonBuffer: add get_mut() --- nested/src/singleton/buffer.rs | 56 ++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 13 deletions(-) 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()); + } +} +