add impls for Arc<dyn View> and Arc<dyn Observer>

This commit is contained in:
Michael Sippel 2020-12-07 18:28:28 +01:00
parent ebd11796ad
commit 2da96c5482
Signed by: senvas
GPG key ID: F96CF119C34B64A6
2 changed files with 58 additions and 39 deletions

View file

@ -59,7 +59,7 @@ async fn main() {
let view = view_port.outer().get_view(); let view = view_port.outer().get_view();
let mut stream = view_port.outer().stream().map({ let mut stream = view_port.outer().stream().map({
move |_| view.read().unwrap().as_ref().unwrap().view(()).unwrap() move |_| view.view(()).unwrap()
}); });
let fut = task::spawn({ let fut = task::spawn({

View file

@ -95,6 +95,63 @@ where T: Send + Sync,
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
use std::ops::Deref;
use std::sync::{Arc, RwLock};
impl<T: View> View for RwLock<T> {
type Key = T::Key;
type Value = T::Value;
fn view(&self, key: T::Key) -> Option<T::Value> {
self.read().unwrap().view(key)
}
}
impl<T: Observer> Observer for RwLock<T> {
type Msg = T::Msg;
fn notify(&self, msg: T::Msg) {
self.read().unwrap().notify(msg)
}
}
impl<T: View> View for Arc<T> {
type Key = T::Key;
type Value = T::Value;
fn view(&self, key: T::Key) -> Option<T::Value> {
self.deref().view(key)
}
}
impl<T: Observer> Observer for Arc<T> {
type Msg = T::Msg;
fn notify(&self, msg: T::Msg) {
self.deref().notify(msg)
}
}
impl<K, V> View for Arc<dyn View<Key = K, Value = V>>
where K: Send + Sync,
V: Send + Sync {
type Key = K;
type Value = V;
fn view(&self, key: K) -> Option<V> {
self.deref().view(key)
}
}
impl<T> Observer for Arc<dyn Observer<Msg = T>>
where T: Send + Sync {
type Msg = T;
fn notify(&self, msg: T) {
self.deref().notify(msg)
}
}
impl<T: View> View for Option<T> { impl<T: View> View for Option<T> {
type Key = T::Key; type Key = T::Key;
type Value = T::Value; type Value = T::Value;
@ -118,41 +175,3 @@ impl<T: Observer> Observer for Option<T> {
} }
} }
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
impl<T: View> View for std::sync::RwLock<T> {
type Key = T::Key;
type Value = T::Value;
fn view(&self, key: T::Key) -> Option<T::Value> {
self.read().unwrap().view(key)
}
}
impl<T: Observer> Observer for std::sync::RwLock<T> {
type Msg = T::Msg;
fn notify(&self, msg: T::Msg) {
self.read().unwrap().notify(msg)
}
}
use std::ops::Deref;
impl<T: View> View for std::sync::Arc<T> {
type Key = T::Key;
type Value = T::Value;
fn view(&self, key: T::Key) -> Option<T::Value> {
self.deref().view(key)
}
}
impl<T: Observer> Observer for std::sync::Arc<T> {
type Msg = T::Msg;
fn notify(&self, msg: T::Msg) {
self.deref().notify(msg)
}
}