move VecBuffer & SingletonBuffer into separate files
This commit is contained in:
parent
852e1807db
commit
4988db36e8
3 changed files with 90 additions and 67 deletions
72
src/main.rs
72
src/main.rs
|
@ -5,6 +5,8 @@
|
|||
pub mod view;
|
||||
pub mod port;
|
||||
pub mod channel;
|
||||
pub mod singleton_buffer;
|
||||
pub mod vec_buffer;
|
||||
|
||||
use {
|
||||
async_std::{
|
||||
|
@ -16,76 +18,12 @@ use {
|
|||
cgmath::{Vector2},
|
||||
crate::{
|
||||
view::{View, Observer},
|
||||
port::{InnerViewPort, OuterViewPort}
|
||||
port::{InnerViewPort, OuterViewPort},
|
||||
singleton_buffer::SingletonBuffer,
|
||||
vec_buffer::VecBuffer
|
||||
}
|
||||
};
|
||||
|
||||
struct SingletonBuffer<T: Clone + Eq + Send + Sync + 'static> {
|
||||
data: Arc<RwLock<Option<T>>>,
|
||||
port: InnerViewPort<(), T>
|
||||
}
|
||||
|
||||
impl<T: Clone + Eq + Send + Sync + 'static> SingletonBuffer<T> {
|
||||
fn new(
|
||||
port: InnerViewPort<(), T>
|
||||
) -> Self {
|
||||
let data = Arc::new(RwLock::new(None));
|
||||
|
||||
port.set_view_fn({
|
||||
let data = data.clone();
|
||||
move |_| data.read().unwrap().clone()
|
||||
});
|
||||
|
||||
SingletonBuffer {
|
||||
data,
|
||||
port
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self, new_value: T) {
|
||||
let mut data = self.data.write().unwrap();
|
||||
if *data != Some(new_value.clone()) {
|
||||
*data = Some(new_value);
|
||||
drop(data);
|
||||
self.port.notify(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
impl<T: Clone + Send + Sync> View for Vec<T> {
|
||||
type Key = usize;
|
||||
type Value = T;
|
||||
|
||||
fn view(&self, key: usize) -> Option<T> {
|
||||
self.get(key).cloned()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct VecBuffer<T: Clone + Eq + Send + Sync + 'static> {
|
||||
data: Arc<RwLock<Vec<T>>>,
|
||||
port: InnerViewPort<usize, T>
|
||||
}
|
||||
|
||||
impl<T: Clone + Eq + Send + Sync + 'static> VecBuffer<T> {
|
||||
fn new(port: InnerViewPort<usize, T>) -> Self {
|
||||
let data = Arc::new(RwLock::new(Vec::new()));
|
||||
port.set_view(data.clone());
|
||||
VecBuffer { data, port }
|
||||
}
|
||||
|
||||
fn push(&mut self, val: T) {
|
||||
self.port.notify({
|
||||
let mut d = self.data.write().unwrap();
|
||||
let len = d.len();
|
||||
d.push(val);
|
||||
len
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[async_std::main]
|
||||
async fn main() {
|
||||
let digits = port::ViewPort::new();
|
||||
|
|
42
src/singleton_buffer.rs
Normal file
42
src/singleton_buffer.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
use {
|
||||
std::{
|
||||
sync::{Arc, RwLock}
|
||||
},
|
||||
crate::{
|
||||
view::Observer,
|
||||
port::InnerViewPort
|
||||
}
|
||||
};
|
||||
|
||||
pub struct SingletonBuffer<T: Clone + Eq + Send + Sync + 'static> {
|
||||
data: Arc<RwLock<Option<T>>>,
|
||||
port: InnerViewPort<(), T>
|
||||
}
|
||||
|
||||
impl<T: Clone + Eq + Send + Sync + 'static> SingletonBuffer<T> {
|
||||
pub fn new(
|
||||
port: InnerViewPort<(), T>
|
||||
) -> Self {
|
||||
let data = Arc::new(RwLock::new(None));
|
||||
|
||||
port.set_view_fn({
|
||||
let data = data.clone();
|
||||
move |_| data.read().unwrap().clone()
|
||||
});
|
||||
|
||||
SingletonBuffer {
|
||||
data,
|
||||
port
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self, new_value: T) {
|
||||
let mut data = self.data.write().unwrap();
|
||||
if *data != Some(new_value.clone()) {
|
||||
*data = Some(new_value);
|
||||
drop(data);
|
||||
self.port.notify(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
43
src/vec_buffer.rs
Normal file
43
src/vec_buffer.rs
Normal file
|
@ -0,0 +1,43 @@
|
|||
use {
|
||||
std::{
|
||||
sync::{Arc, RwLock}
|
||||
},
|
||||
crate::{
|
||||
view::{View, Observer},
|
||||
port::{InnerViewPort}
|
||||
}
|
||||
};
|
||||
|
||||
impl<T: Clone + Send + Sync> View for Vec<T> {
|
||||
type Key = usize;
|
||||
type Value = T;
|
||||
|
||||
fn view(&self, key: usize) -> Option<T> {
|
||||
self.get(key).cloned()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct VecBuffer<T: Clone + Eq + Send + Sync + 'static> {
|
||||
data: Arc<RwLock<Vec<T>>>,
|
||||
port: InnerViewPort<usize, T>
|
||||
}
|
||||
|
||||
impl<T: Clone + Eq + Send + Sync + 'static> VecBuffer<T> {
|
||||
pub fn new(port: InnerViewPort<usize, T>) -> Self {
|
||||
let data = Arc::new(RwLock::new(Vec::new()));
|
||||
port.set_view(data.clone());
|
||||
VecBuffer { data, port }
|
||||
}
|
||||
|
||||
pub fn push(&mut self, val: T) {
|
||||
self.port.notify({
|
||||
let mut d = self.data.write().unwrap();
|
||||
let len = d.len();
|
||||
d.push(val);
|
||||
len
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: add functions
|
||||
}
|
||||
|
Loading…
Reference in a new issue