From 50576e936b2db87a8e6dd8f5a91546b8d1f5403d Mon Sep 17 00:00:00 2001 From: Matt Hunzinger Date: Sat, 20 Jan 2024 01:07:22 -0500 Subject: [PATCH] Clean up --- crates/concoct/src/body.rs | 10 +++++++- crates/concoct/src/hook/use_state.rs | 2 +- crates/concoct/src/lib.rs | 37 +++++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/crates/concoct/src/body.rs b/crates/concoct/src/body.rs index c4e6bfb1..674bff40 100644 --- a/crates/concoct/src/body.rs +++ b/crates/concoct/src/body.rs @@ -1,5 +1,5 @@ use crate::{Node, Tree, View}; -use std::{cell::RefCell, rc::Rc}; +use std::{cell::RefCell, hash::Hash, rc::Rc}; pub trait Body: 'static { fn into_tree(self) -> impl Tree; @@ -11,6 +11,14 @@ impl Body for Option { } } +impl Body for Vec<(K, B)> { + fn into_tree(self) -> impl Tree { + self.into_iter() + .map(|(key, body)| (key, body.into_tree())) + .collect::>() + } +} + pub struct Child { cell: Rc>>, } diff --git a/crates/concoct/src/hook/use_state.rs b/crates/concoct/src/hook/use_state.rs index be932033..816bad4f 100644 --- a/crates/concoct/src/hook/use_state.rs +++ b/crates/concoct/src/hook/use_state.rs @@ -2,7 +2,7 @@ use super::use_ref; use crate::Context; use std::{cell::RefCell, rc::Rc}; -pub fn use_state(make_value: impl FnOnce() -> T) -> (T, Rc) { +pub fn use_state(make_value: impl FnOnce() -> T) -> (T, Rc) { let cell = use_ref(|| RefCell::new(make_value())); let getter = cell.borrow().clone(); diff --git a/crates/concoct/src/lib.rs b/crates/concoct/src/lib.rs index e5489ee8..c8a9e60b 100644 --- a/crates/concoct/src/lib.rs +++ b/crates/concoct/src/lib.rs @@ -1,7 +1,8 @@ use slotmap::{DefaultKey, SlotMap}; use std::any::{Any, TypeId}; use std::borrow::Cow; -use std::collections::{HashMap, VecDeque}; +use std::collections::{HashMap, HashSet, VecDeque}; +use std::hash::Hash; use std::task::{Poll, Waker}; use std::{cell::RefCell, mem, rc::Rc}; @@ -224,6 +225,40 @@ impl Tree for Empty { fn remove(&mut self) {} } +impl Tree for Vec<(K, T)> { + fn build(&mut self) { + for (_, body) in self.iter_mut() { + body.build() + } + } + + fn rebuild(&mut self, last: &mut dyn Any) { + let mut visited = HashSet::new(); + let last = last.downcast_mut::().unwrap(); + + for (key, body) in self.iter_mut() { + if let Some((_, last_body)) = last.iter_mut().find(|(last_key, _)| last_key == key) { + body.rebuild(last_body); + visited.insert(key); + } else { + body.build(); + } + } + + for (key, body) in last.iter_mut() { + if !visited.contains(key) { + body.remove(); + } + } + } + + fn remove(&mut self) { + for (_, body) in self.iter_mut() { + body.remove() + } + } +} + impl Tree for Node where V: View,