MapIndexItem: pass key and item to function

This commit is contained in:
Michael Sippel 2021-01-18 16:59:35 +01:00
parent fffb23cd12
commit d1c523335b
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -20,7 +20,7 @@ pub use {
impl<Key: 'static, Item: 'static> OuterViewPort<dyn IndexView<Key, Item = Item>> { impl<Key: 'static, Item: 'static> OuterViewPort<dyn IndexView<Key, Item = Item>> {
pub fn map_item< pub fn map_item<
DstItem: 'static, DstItem: 'static,
F: Fn(&Item) -> DstItem + Send + Sync + 'static F: Fn(&Key, &Item) -> DstItem + Send + Sync + 'static
>( >(
&self, &self,
f: F f: F
@ -34,7 +34,7 @@ impl<Key: 'static, Item: 'static> OuterViewPort<dyn IndexView<Key, Item = Item>>
pub struct MapIndexItem<Key, DstItem, SrcView, F> pub struct MapIndexItem<Key, DstItem, SrcView, F>
where SrcView: IndexView<Key> + ?Sized, where SrcView: IndexView<Key> + ?Sized,
F: Fn(&SrcView::Item) -> DstItem + Send + Sync F: Fn(&Key, &SrcView::Item) -> DstItem + Send + Sync
{ {
src_view: Option<Arc<SrcView>>, src_view: Option<Arc<SrcView>>,
f: F, f: F,
@ -45,7 +45,7 @@ impl<Key, DstItem, SrcView, F> MapIndexItem<Key, DstItem, SrcView, F>
where Key: 'static, where Key: 'static,
DstItem: 'static, DstItem: 'static,
SrcView: IndexView<Key> + ?Sized + 'static, SrcView: IndexView<Key> + ?Sized + 'static,
F: Fn(&SrcView::Item) -> DstItem + Send + Sync + 'static F: Fn(&Key, &SrcView::Item) -> DstItem + Send + Sync + 'static
{ {
fn new( fn new(
port: InnerViewPort<dyn IndexView<Key, Item = DstItem>>, port: InnerViewPort<dyn IndexView<Key, Item = DstItem>>,
@ -66,19 +66,19 @@ where Key: 'static,
impl<Key, DstItem, SrcView, F> View for MapIndexItem<Key, DstItem, SrcView, F> impl<Key, DstItem, SrcView, F> View for MapIndexItem<Key, DstItem, SrcView, F>
where SrcView: IndexView<Key> + ?Sized, where SrcView: IndexView<Key> + ?Sized,
F: Fn(&SrcView::Item) -> DstItem + Send + Sync F: Fn(&Key, &SrcView::Item) -> DstItem + Send + Sync
{ {
type Msg = Key; type Msg = Key;
} }
impl<Key, DstItem, SrcView, F> IndexView<Key> for MapIndexItem<Key, DstItem, SrcView, F> impl<Key, DstItem, SrcView, F> IndexView<Key> for MapIndexItem<Key, DstItem, SrcView, F>
where SrcView: IndexView<Key> + ?Sized, where SrcView: IndexView<Key> + ?Sized,
F: Fn(&SrcView::Item) -> DstItem + Send + Sync F: Fn(&Key, &SrcView::Item) -> DstItem + Send + Sync
{ {
type Item = DstItem; type Item = DstItem;
fn get(&self, key: &Key) -> Option<Self::Item> { fn get(&self, key: &Key) -> Option<Self::Item> {
self.src_view.as_ref()?.get(key).as_ref().map(&self.f) self.src_view.as_ref()?.get(key).as_ref().map(|item| (self.f)(key, item))
} }
fn area(&self) -> Option<Vec<Key>> { fn area(&self) -> Option<Vec<Key>> {
@ -88,7 +88,7 @@ where SrcView: IndexView<Key> + ?Sized,
impl<Key, DstItem, SrcView, F> Observer<SrcView> for MapIndexItem<Key, DstItem, SrcView, F> impl<Key, DstItem, SrcView, F> Observer<SrcView> for MapIndexItem<Key, DstItem, SrcView, F>
where SrcView: IndexView<Key> + ?Sized, where SrcView: IndexView<Key> + ?Sized,
F: Fn(&SrcView::Item) -> DstItem + Send + Sync F: Fn(&Key, &SrcView::Item) -> DstItem + Send + Sync
{ {
fn reset(&mut self, view: Option<Arc<SrcView>>) { fn reset(&mut self, view: Option<Arc<SrcView>>) {
let old_area = self.area(); let old_area = self.area();