From 6aef446134cabfdfffef71f32990bc17aac5c665 Mon Sep 17 00:00:00 2001 From: Michael Sippel Date: Tue, 19 Jan 2021 22:54:50 +0100 Subject: [PATCH] impl View traits for Option --- src/core/view.rs | 7 +++++-- src/index/map_item.rs | 4 ++-- src/index/map_key.rs | 4 ++-- src/index/mod.rs | 20 ++++++++++++++++++-- src/sequence/mod.rs | 19 +++++++++++++++++-- src/sequence/seq2idx.rs | 5 ++--- src/string_editor.rs | 21 +++++---------------- 7 files changed, 51 insertions(+), 29 deletions(-) diff --git a/src/core/view.rs b/src/core/view.rs index 1f76892..bfe8274 100644 --- a/src/core/view.rs +++ b/src/core/view.rs @@ -13,12 +13,15 @@ pub trait View : Send + Sync { use std::sync::{Arc, RwLock}; -impl View for RwLock { +impl View for RwLock { type Msg = V::Msg; } -impl View for Arc { +impl View for Arc { type Msg = V::Msg; } +impl View for Option { + type Msg = V::Msg; +} diff --git a/src/index/map_item.rs b/src/index/map_item.rs index 28bc582..4493d59 100644 --- a/src/index/map_item.rs +++ b/src/index/map_item.rs @@ -78,11 +78,11 @@ where SrcView: IndexView + ?Sized, type Item = DstItem; fn get(&self, key: &Key) -> Option { - 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> { - self.src_view.as_ref()?.area() + self.src_view.area() } } diff --git a/src/index/map_key.rs b/src/index/map_key.rs index 8138c3b..3b33f2d 100644 --- a/src/index/map_key.rs +++ b/src/index/map_key.rs @@ -88,11 +88,11 @@ where SrcView: IndexView + ?Sized, type Item = SrcView::Item; fn get(&self, key: &DstKey) -> Option { - self.src_view.as_ref()?.get(&(self.f2)(key)?) + self.src_view.get(&(self.f2)(key)?) } fn area(&self) -> Option> { - Some(self.src_view.as_ref()?.area()?.iter().map(&self.f1).collect()) + Some(self.src_view.area()?.iter().map(&self.f1).collect()) } } diff --git a/src/index/mod.rs b/src/index/mod.rs index 8bcf20b..96fcbd4 100644 --- a/src/index/mod.rs +++ b/src/index/mod.rs @@ -25,7 +25,7 @@ pub trait IndexView : View { //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> -impl> IndexView for RwLock { +impl + ?Sized> IndexView for RwLock { type Item = V::Item; fn get(&self, key: &Key) -> Option { @@ -37,7 +37,7 @@ impl> IndexView for RwLock { } } -impl> IndexView for Arc { +impl + ?Sized> IndexView for Arc { type Item = V::Item; fn get(&self, key: &Key) -> Option { @@ -49,6 +49,22 @@ impl> IndexView for Arc { } } +impl> IndexView for Option { + type Item = V::Item; + + fn get(&self, key: &Key) -> Option { + (self.as_ref()? as &V).get(key) + } + + fn area(&self) -> Option> { + if let Some(v) = self.as_ref() { + v.area() + } else { + Some(Vec::new()) + } + } +} + //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> pub trait ImplIndexView : Send + Sync { diff --git a/src/sequence/mod.rs b/src/sequence/mod.rs index e80e98a..bb09a04 100644 --- a/src/sequence/mod.rs +++ b/src/sequence/mod.rs @@ -25,7 +25,7 @@ use std::{ ops::{Deref} }; -impl SequenceView for RwLock { +impl SequenceView for RwLock { type Item = V::Item; fn get(&self, idx: &usize) -> Option { @@ -37,7 +37,7 @@ impl SequenceView for RwLock { } } -impl SequenceView for Arc { +impl SequenceView for Arc { type Item = V::Item; fn get(&self, idx: &usize) -> Option { @@ -49,4 +49,19 @@ impl SequenceView for Arc { } } +impl SequenceView for Option { + type Item = V::Item; + + fn get(&self, idx: &usize) -> Option { + (self.as_ref()? as &V).get(idx) + } + + fn len(&self) -> Option { + if let Some(v) = self.as_ref() { + v.len() + } else { + Some(0) + } + } +} diff --git a/src/sequence/seq2idx.rs b/src/sequence/seq2idx.rs index c42af0b..0f0ad0b 100644 --- a/src/sequence/seq2idx.rs +++ b/src/sequence/seq2idx.rs @@ -53,12 +53,11 @@ where SrcView: SequenceView + ?Sized + 'static { type Item = SrcView::Item; fn get(&self, key: &usize) -> Option { - self.src_view.as_ref()?.get(key) + self.src_view.get(key) } fn area(&self) -> Option> { - let len = self.src_view.as_ref()?.len()?; - Some((0 .. len).collect()) + Some((0 .. self.src_view.len()?).collect()) } } diff --git a/src/string_editor.rs b/src/string_editor.rs index 07d62e0..d270be9 100644 --- a/src/string_editor.rs +++ b/src/string_editor.rs @@ -9,6 +9,7 @@ use { //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> +#[derive(Clone)] pub struct StringEditor { data: VecBuffer, cursor: SingletonBuffer, @@ -34,11 +35,11 @@ impl StringEditor { pub fn get_data_port(&self) -> OuterViewPort>> { self.data_port.outer() } - + pub fn get_cursor_port(&self) -> OuterViewPort> { self.cursor_port.outer() } - + pub fn goto(&mut self, new_pos: usize) { if new_pos <= self.data.len() { self.cursor.set(new_pos); @@ -123,21 +124,9 @@ pub mod insert_view { impl Observer> for DataObserver { fn reset(&mut self, new_data: Option>>) { - let old_len = - if let Some(data) = self.data.as_ref() { - data.len().unwrap_or(0) - } else { - 0 - }; - + let old_len = self.data.len().unwrap_or(0); self.data = new_data; - - let new_len = - if let Some(data) = self.data.as_ref() { - data.len().unwrap_or(0) - } else { - 0 - }; + let new_len = self.data.len().unwrap_or(0); self.edit .upgrade().unwrap()