impl View traits for Option<V>

This commit is contained in:
Michael Sippel 2021-01-19 22:54:50 +01:00
parent c2b4683a1a
commit 6aef446134
Signed by: senvas
GPG key ID: F96CF119C34B64A6
7 changed files with 51 additions and 29 deletions

View file

@ -13,12 +13,15 @@ pub trait View : Send + Sync {
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
impl<V: View> View for RwLock<V> { impl<V: View + ?Sized> View for RwLock<V> {
type Msg = V::Msg; type Msg = V::Msg;
} }
impl<V: View> View for Arc<V> { impl<V: View + ?Sized> View for Arc<V> {
type Msg = V::Msg; type Msg = V::Msg;
} }
impl<V: View> View for Option<V> {
type Msg = V::Msg;
}

View file

@ -78,11 +78,11 @@ where SrcView: IndexView<Key> + ?Sized,
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(|item| (self.f)(key, item)) self.src_view.get(key).as_ref().map(|item| (self.f)(key, item))
} }
fn area(&self) -> Option<Vec<Key>> { fn area(&self) -> Option<Vec<Key>> {
self.src_view.as_ref()?.area() self.src_view.area()
} }
} }

View file

@ -88,11 +88,11 @@ where SrcView: IndexView<SrcKey> + ?Sized,
type Item = SrcView::Item; type Item = SrcView::Item;
fn get(&self, key: &DstKey) -> Option<Self::Item> { fn get(&self, key: &DstKey) -> Option<Self::Item> {
self.src_view.as_ref()?.get(&(self.f2)(key)?) self.src_view.get(&(self.f2)(key)?)
} }
fn area(&self) -> Option<Vec<DstKey>> { fn area(&self) -> Option<Vec<DstKey>> {
Some(self.src_view.as_ref()?.area()?.iter().map(&self.f1).collect()) Some(self.src_view.area()?.iter().map(&self.f1).collect())
} }
} }

View file

@ -25,7 +25,7 @@ pub trait IndexView<Key> : View<Msg = Key> {
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
impl<Key, V: IndexView<Key>> IndexView<Key> for RwLock<V> { impl<Key, V: IndexView<Key> + ?Sized> IndexView<Key> for RwLock<V> {
type Item = V::Item; type Item = V::Item;
fn get(&self, key: &Key) -> Option<Self::Item> { fn get(&self, key: &Key) -> Option<Self::Item> {
@ -37,7 +37,7 @@ impl<Key, V: IndexView<Key>> IndexView<Key> for RwLock<V> {
} }
} }
impl<Key, V: IndexView<Key>> IndexView<Key> for Arc<V> { impl<Key, V: IndexView<Key> + ?Sized> IndexView<Key> for Arc<V> {
type Item = V::Item; type Item = V::Item;
fn get(&self, key: &Key) -> Option<Self::Item> { fn get(&self, key: &Key) -> Option<Self::Item> {
@ -49,6 +49,22 @@ impl<Key, V: IndexView<Key>> IndexView<Key> for Arc<V> {
} }
} }
impl<Key, V: IndexView<Key>> IndexView<Key> for Option<V> {
type Item = V::Item;
fn get(&self, key: &Key) -> Option<Self::Item> {
(self.as_ref()? as &V).get(key)
}
fn area(&self) -> Option<Vec<Key>> {
if let Some(v) = self.as_ref() {
v.area()
} else {
Some(Vec::new())
}
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub trait ImplIndexView : Send + Sync { pub trait ImplIndexView : Send + Sync {

View file

@ -25,7 +25,7 @@ use std::{
ops::{Deref} ops::{Deref}
}; };
impl<V: SequenceView> SequenceView for RwLock<V> { impl<V: SequenceView + ?Sized> SequenceView for RwLock<V> {
type Item = V::Item; type Item = V::Item;
fn get(&self, idx: &usize) -> Option<Self::Item> { fn get(&self, idx: &usize) -> Option<Self::Item> {
@ -37,7 +37,7 @@ impl<V: SequenceView> SequenceView for RwLock<V> {
} }
} }
impl<V: SequenceView> SequenceView for Arc<V> { impl<V: SequenceView + ?Sized> SequenceView for Arc<V> {
type Item = V::Item; type Item = V::Item;
fn get(&self, idx: &usize) -> Option<Self::Item> { fn get(&self, idx: &usize) -> Option<Self::Item> {
@ -49,4 +49,19 @@ impl<V: SequenceView> SequenceView for Arc<V> {
} }
} }
impl<V: SequenceView> SequenceView for Option<V> {
type Item = V::Item;
fn get(&self, idx: &usize) -> Option<Self::Item> {
(self.as_ref()? as &V).get(idx)
}
fn len(&self) -> Option<usize> {
if let Some(v) = self.as_ref() {
v.len()
} else {
Some(0)
}
}
}

View file

@ -53,12 +53,11 @@ where SrcView: SequenceView + ?Sized + 'static {
type Item = SrcView::Item; type Item = SrcView::Item;
fn get(&self, key: &usize) -> Option<Self::Item> { fn get(&self, key: &usize) -> Option<Self::Item> {
self.src_view.as_ref()?.get(key) self.src_view.get(key)
} }
fn area(&self) -> Option<Vec<usize>> { fn area(&self) -> Option<Vec<usize>> {
let len = self.src_view.as_ref()?.len()?; Some((0 .. self.src_view.len()?).collect())
Some((0 .. len).collect())
} }
} }

View file

@ -9,6 +9,7 @@ use {
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
#[derive(Clone)]
pub struct StringEditor { pub struct StringEditor {
data: VecBuffer<char>, data: VecBuffer<char>,
cursor: SingletonBuffer<usize>, cursor: SingletonBuffer<usize>,
@ -34,11 +35,11 @@ impl StringEditor {
pub fn get_data_port(&self) -> OuterViewPort<RwLock<Vec<char>>> { pub fn get_data_port(&self) -> OuterViewPort<RwLock<Vec<char>>> {
self.data_port.outer() self.data_port.outer()
} }
pub fn get_cursor_port(&self) -> OuterViewPort<dyn SingletonView<Item = usize>> { pub fn get_cursor_port(&self) -> OuterViewPort<dyn SingletonView<Item = usize>> {
self.cursor_port.outer() self.cursor_port.outer()
} }
pub fn goto(&mut self, new_pos: usize) { pub fn goto(&mut self, new_pos: usize) {
if new_pos <= self.data.len() { if new_pos <= self.data.len() {
self.cursor.set(new_pos); self.cursor.set(new_pos);
@ -123,21 +124,9 @@ pub mod insert_view {
impl Observer<dyn SequenceView<Item = char>> for DataObserver { impl Observer<dyn SequenceView<Item = char>> for DataObserver {
fn reset(&mut self, new_data: Option<Arc<dyn SequenceView<Item = char>>>) { fn reset(&mut self, new_data: Option<Arc<dyn SequenceView<Item = char>>>) {
let old_len = let old_len = self.data.len().unwrap_or(0);
if let Some(data) = self.data.as_ref() {
data.len().unwrap_or(0)
} else {
0
};
self.data = new_data; self.data = new_data;
let new_len = self.data.len().unwrap_or(0);
let new_len =
if let Some(data) = self.data.as_ref() {
data.len().unwrap_or(0)
} else {
0
};
self.edit self.edit
.upgrade().unwrap() .upgrade().unwrap()