vec buffer: add constructor from data-arc

This commit is contained in:
Michael Sippel 2024-06-06 14:45:08 +02:00
parent 81bd253dd6
commit 10c63e8ae5
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -83,8 +83,7 @@ impl<T> VecBuffer<T>
where where
T: Clone + Send + Sync + 'static, T: Clone + Send + Sync + 'static,
{ {
pub fn with_data_port(data: Vec<T>, port: InnerViewPort<RwLock<Vec<T>>>) -> Self { pub fn with_data_arc_port(data: Arc<RwLock<Vec<T>>>, port: InnerViewPort<RwLock<Vec<T>>>) -> Self {
let data = Arc::new(RwLock::new(data));
port.set_view(Some(data.clone())); port.set_view(Some(data.clone()));
for x in data.read().unwrap().iter().cloned() { for x in data.read().unwrap().iter().cloned() {
@ -97,6 +96,11 @@ where
} }
} }
pub fn with_data_port(data: Vec<T>, port: InnerViewPort<RwLock<Vec<T>>>) -> Self {
let data = Arc::new(RwLock::new(data));
Self::with_data_arc_port( data, port )
}
pub fn attach_to(&self, port: OuterViewPort< dyn ListView<T> >) -> Arc<RwLock<VecBufferTarget<T>>> { pub fn attach_to(&self, port: OuterViewPort< dyn ListView<T> >) -> Arc<RwLock<VecBufferTarget<T>>> {
self.port.0.add_update_hook(Arc::new(port.0.clone())); self.port.0.add_update_hook(Arc::new(port.0.clone()));
@ -271,7 +275,7 @@ mod tests {
buf.push('b'); buf.push('b');
list_view.0.update(); buf2.get_port().0.update();
assert_eq!(buf2.len(), 2); assert_eq!(buf2.len(), 2);
assert_eq!(buf2.get(0), 'a'); assert_eq!(buf2.get(0), 'a');
assert_eq!(buf2.get(1), 'b'); assert_eq!(buf2.get(1), 'b');
@ -279,7 +283,7 @@ mod tests {
buf.push('c'); buf.push('c');
buf.remove(0); buf.remove(0);
list_view.0.update(); buf2.get_port().0.update();
assert_eq!(buf2.len(), 2); assert_eq!(buf2.len(), 2);
assert_eq!(buf2.get(0), 'b'); assert_eq!(buf2.get(0), 'b');
assert_eq!(buf2.get(1), 'c'); assert_eq!(buf2.get(1), 'c');