From a79527051579bdf629f0e872d6779c79bbe2d402 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Fri, 9 Aug 2024 02:06:35 +0200 Subject: [PATCH] add vec2sgl projection --- src/projection/mod.rs | 1 + src/projection/vec2sgl.rs | 85 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 src/projection/vec2sgl.rs diff --git a/src/projection/mod.rs b/src/projection/mod.rs index 899ca12..f43f610 100644 --- a/src/projection/mod.rs +++ b/src/projection/mod.rs @@ -3,6 +3,7 @@ pub mod projection_helper; pub mod sgl2idx; pub mod sgl2seq; +pub mod vec2sgl; pub mod vec2seq; pub mod vec2bin; pub mod vec2json; diff --git a/src/projection/vec2sgl.rs b/src/projection/vec2sgl.rs new file mode 100644 index 0000000..1447630 --- /dev/null +++ b/src/projection/vec2sgl.rs @@ -0,0 +1,85 @@ +use { + crate::{ + view::{ + InnerViewPort, Observer, ObserverBroadcast, ObserverExt, OuterViewPort, View, ViewPort, + sequence::SequenceView, + singleton::SingletonView + }, + buffer::vec::VecDiff, + }, + std::sync::Arc, + std::sync::RwLock, +}; + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +/// Adapter View implementing `Singleton` for `Vec` +pub struct VecSingleton +where + T: Clone + Send + Sync + 'static, +{ + data: Option>>>, + cast: Arc>>>>, +} + +impl VecSingleton +where + T: Clone + Send + Sync + 'static, +{ + pub fn new(port: InnerViewPort>>) -> Arc> { + let sgl = Arc::new(RwLock::new(VecSingleton { + data: None, + cast: port.get_broadcast(), + })); + port.set_view(Some(sgl.clone())); + sgl + } +} + +impl Observer>> for VecSingleton +where + T: Clone + Send + Sync + 'static, +{ + fn reset(&mut self, view: Option>>>) { + self.data = view; + self.cast.notify(&()); + } + + fn notify(&mut self, _diff: &VecDiff) { + self.cast.notify(&()); + } +} + +impl View for VecSingleton +where + T: Clone + Send + Sync + 'static, +{ + type Msg = (); +} + +impl SingletonView for VecSingleton +where + T: Clone + Send + Sync + 'static, +{ + type Item = Vec; + + fn get(&self) -> Vec { + self.data.as_ref().unwrap().read().unwrap().clone() + } +} + +//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> + +impl OuterViewPort>> +where + T: Clone + Send + Sync + 'static, +{ + pub fn to_singleton(&self) -> OuterViewPort>> { + let port = ViewPort::new(); + port.add_update_hook(Arc::new(self.0.clone())); + + let vec_sgl = VecSingleton::new(port.inner()); + self.add_observer(vec_sgl.clone()); + port.into_outer() + } +}