lib-nested/nested/src/index/mod.rs

108 lines
2 KiB
Rust
Raw Normal View History

2021-01-12 23:13:27 +01:00
pub mod map_item;
pub mod map_key;
2021-08-27 22:31:25 +02:00
pub mod buffer;
2021-01-12 23:13:27 +01:00
2021-01-06 21:35:46 +01:00
use {
std::{
sync::Arc,
ops::Deref,
2021-01-06 21:35:46 +01:00
},
std::sync::RwLock,
2021-01-06 21:35:46 +01:00
crate::core::View
};
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub trait IndexView<Key> : View<Msg = Key>
where Key: Send + Sync {
2021-01-06 21:35:46 +01:00
type Item;
fn get(&self, key: &Key) -> Option<Self::Item>;
2021-01-06 21:35:46 +01:00
2021-01-16 15:31:37 +01:00
// todo: AreaIterator enum to switch between Allocated and Procedural area
fn area(&self) -> Option<Vec<Key>> {
2021-01-06 21:35:46 +01:00
None
}
}
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
impl<Key, V> IndexView<Key> for RwLock<V>
where Key: Send + Sync,
V: IndexView<Key> + ?Sized
{
2021-01-06 21:35:46 +01:00
type Item = V::Item;
fn get(&self, key: &Key) -> Option<Self::Item> {
2021-01-06 21:35:46 +01:00
self.read().unwrap().get(key)
}
fn area(&self) -> Option<Vec<Key>> {
self.read().unwrap().area()
2021-01-06 21:35:46 +01:00
}
}
impl<Key, V> IndexView<Key> for Arc<V>
where Key: Send + Sync,
V: IndexView<Key> + ?Sized
{
2021-01-06 21:35:46 +01:00
type Item = V::Item;
fn get(&self, key: &Key) -> Option<Self::Item> {
2021-01-06 21:35:46 +01:00
self.deref().get(key)
}
fn area(&self) -> Option<Vec<Key>> {
self.deref().area()
2021-01-06 21:35:46 +01:00
}
}
impl<Key, V> IndexView<Key> for Option<V>
where Key: Send + Sync,
V: IndexView<Key>
{
2021-01-19 22:54:50 +01:00
type Item = V::Item;
fn get(&self, key: &Key) -> Option<Self::Item> {
self.as_ref()?.get(key)
2021-01-19 22:54:50 +01:00
}
fn area(&self) -> Option<Vec<Key>> {
if let Some(v) = self.as_ref() {
v.area()
} else {
Some(Vec::new())
}
}
}
2021-01-06 21:35:46 +01:00
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub trait ImplIndexView : Send + Sync {
type Key : Send + Sync;
2021-01-06 21:35:46 +01:00
type Value;
fn get(&self, key: &Self::Key) -> Option<Self::Value>;
fn area(&self) -> Option<Vec<Self::Key>> {
2021-01-06 21:35:46 +01:00
None
}
}
impl<V: ImplIndexView> View for V {
type Msg = V::Key;
}
impl<V: ImplIndexView> IndexView<V::Key> for V {
type Item = V::Value;
fn get(&self, key: &V::Key) -> Option<Self::Item> {
2021-01-06 21:35:46 +01:00
(self as &V).get(key)
}
fn area(&self) -> Option<Vec<V::Key>> {
(self as &V).area()
2021-01-06 21:35:46 +01:00
}
}