2021-01-12 23:13:27 +01:00
|
|
|
pub use {
|
|
|
|
std::{
|
2021-03-30 22:47:05 +02:00
|
|
|
sync::Arc,
|
2021-01-16 13:57:06 +01:00
|
|
|
boxed::Box
|
2021-01-12 23:13:27 +01:00
|
|
|
},
|
2021-03-30 22:47:05 +02:00
|
|
|
std::sync::RwLock,
|
2021-01-12 23:13:27 +01:00
|
|
|
crate::{
|
|
|
|
core::{
|
|
|
|
View,
|
|
|
|
Observer,
|
2021-01-16 13:57:06 +01:00
|
|
|
ObserverExt,
|
2021-01-12 23:13:27 +01:00
|
|
|
ObserverBroadcast,
|
|
|
|
ViewPort,
|
|
|
|
InnerViewPort,
|
|
|
|
OuterViewPort
|
|
|
|
},
|
|
|
|
index::{IndexView}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
impl<Key: 'static, Item: 'static> OuterViewPort<dyn IndexView<Key, Item = Item>> {
|
|
|
|
pub fn map_item<
|
2021-01-16 20:19:52 +01:00
|
|
|
DstItem: 'static,
|
2021-01-18 16:59:35 +01:00
|
|
|
F: Fn(&Key, &Item) -> DstItem + Send + Sync + 'static
|
2021-01-12 23:13:27 +01:00
|
|
|
>(
|
|
|
|
&self,
|
|
|
|
f: F
|
|
|
|
) -> OuterViewPort<dyn IndexView<Key, Item = DstItem>> {
|
|
|
|
let port = ViewPort::new();
|
|
|
|
let map = MapIndexItem::new(port.inner(), f);
|
|
|
|
self.add_observer(map.clone());
|
|
|
|
port.into_outer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MapIndexItem<Key, DstItem, SrcView, F>
|
|
|
|
where SrcView: IndexView<Key> + ?Sized,
|
2021-01-18 16:59:35 +01:00
|
|
|
F: Fn(&Key, &SrcView::Item) -> DstItem + Send + Sync
|
2021-01-12 23:13:27 +01:00
|
|
|
{
|
|
|
|
src_view: Option<Arc<SrcView>>,
|
|
|
|
f: F,
|
|
|
|
cast: Arc<RwLock<ObserverBroadcast<dyn IndexView<Key, Item = DstItem>>>>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Key, DstItem, SrcView, F> MapIndexItem<Key, DstItem, SrcView, F>
|
|
|
|
where Key: 'static,
|
2021-01-16 20:19:52 +01:00
|
|
|
DstItem: 'static,
|
2021-01-12 23:13:27 +01:00
|
|
|
SrcView: IndexView<Key> + ?Sized + 'static,
|
2021-01-18 16:59:35 +01:00
|
|
|
F: Fn(&Key, &SrcView::Item) -> DstItem + Send + Sync + 'static
|
2021-01-12 23:13:27 +01:00
|
|
|
{
|
|
|
|
fn new(
|
|
|
|
port: InnerViewPort<dyn IndexView<Key, Item = DstItem>>,
|
|
|
|
f: F
|
|
|
|
) -> Arc<RwLock<Self>> {
|
|
|
|
let map = Arc::new(RwLock::new(
|
|
|
|
MapIndexItem {
|
|
|
|
src_view: None,
|
|
|
|
f,
|
|
|
|
cast: port.get_broadcast()
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
|
|
|
port.set_view(Some(map.clone()));
|
|
|
|
map
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Key, DstItem, SrcView, F> View for MapIndexItem<Key, DstItem, SrcView, F>
|
|
|
|
where SrcView: IndexView<Key> + ?Sized,
|
2021-01-18 16:59:35 +01:00
|
|
|
F: Fn(&Key, &SrcView::Item) -> DstItem + Send + Sync
|
2021-01-12 23:13:27 +01:00
|
|
|
{
|
|
|
|
type Msg = Key;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Key, DstItem, SrcView, F> IndexView<Key> for MapIndexItem<Key, DstItem, SrcView, F>
|
2021-01-16 20:19:52 +01:00
|
|
|
where SrcView: IndexView<Key> + ?Sized,
|
2021-01-18 16:59:35 +01:00
|
|
|
F: Fn(&Key, &SrcView::Item) -> DstItem + Send + Sync
|
2021-01-12 23:13:27 +01:00
|
|
|
{
|
|
|
|
type Item = DstItem;
|
|
|
|
|
2021-01-16 20:19:52 +01:00
|
|
|
fn get(&self, key: &Key) -> Option<Self::Item> {
|
2021-01-19 22:54:50 +01:00
|
|
|
self.src_view.get(key).as_ref().map(|item| (self.f)(key, item))
|
2021-01-12 23:13:27 +01:00
|
|
|
}
|
|
|
|
|
2021-01-16 13:57:06 +01:00
|
|
|
fn area(&self) -> Option<Vec<Key>> {
|
2021-01-19 22:54:50 +01:00
|
|
|
self.src_view.area()
|
2021-01-12 23:13:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Key, DstItem, SrcView, F> Observer<SrcView> for MapIndexItem<Key, DstItem, SrcView, F>
|
2021-01-16 20:19:52 +01:00
|
|
|
where SrcView: IndexView<Key> + ?Sized,
|
2021-01-18 16:59:35 +01:00
|
|
|
F: Fn(&Key, &SrcView::Item) -> DstItem + Send + Sync
|
2021-01-12 23:13:27 +01:00
|
|
|
{
|
|
|
|
fn reset(&mut self, view: Option<Arc<SrcView>>) {
|
2021-01-16 13:57:06 +01:00
|
|
|
let old_area = self.area();
|
2021-01-12 23:13:27 +01:00
|
|
|
self.src_view = view;
|
2021-01-16 13:57:06 +01:00
|
|
|
let new_area = self.area();
|
|
|
|
|
|
|
|
if let Some(area) = old_area { self.cast.notify_each(area); }
|
|
|
|
if let Some(area) = new_area { self.cast.notify_each(area); }
|
2021-01-12 23:13:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn notify(&self, msg: &Key) {
|
|
|
|
self.cast.notify(msg);
|
|
|
|
}
|
|
|
|
}
|
2021-01-16 13:57:06 +01:00
|
|
|
|