Skip to content

Commit

Permalink
Simplify composable trait
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Nov 25, 2023
1 parent 0662492 commit 8686777
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 40 deletions.
8 changes: 1 addition & 7 deletions src/any_composable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@ use std::any::Any;

pub trait AnyComposable {
fn any_build(&mut self, cx: &mut BuildContext) -> Box<dyn Any>;

fn any_rebuild(&mut self, state: &mut dyn Any);
}

impl<C: Composable> AnyComposable for C {
fn any_build(&mut self, cx: &mut BuildContext) -> Box<dyn Any> {
Box::new(self.build(cx))
}

fn any_rebuild(&mut self, state: &mut dyn Any) {
self.rebuild(state.downcast_mut().unwrap())
Box::new(self.compose(cx))
}
}
30 changes: 8 additions & 22 deletions src/composable.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,28 @@
use impl_trait_for_tuples::impl_for_tuples;

use crate::BuildContext;
use crate::{use_ref, BuildContext};

/// Composable object that handles diffing.
pub trait Composable {
type State: 'static;

fn build(&mut self, cx: &mut BuildContext) -> Self::State;

fn rebuild(&mut self, state: &mut Self::State);
fn compose(&mut self, cx: &mut BuildContext);
}

impl<F, C> Composable for F
where
F: FnMut() -> C + Clone + 'static,
C: Composable + 'static,
{
type State = ();

fn build(&mut self, cx: &mut BuildContext) -> Self::State {
fn compose(&mut self, cx: &mut BuildContext) {
let mut f = self.clone();
cx.insert(Box::new(move || Box::new(f())));
use_ref(|| {
cx.insert(Box::new(move || Box::new(f())));
});
}

fn rebuild(&mut self, _state: &mut Self::State) {}
}

#[impl_for_tuples(16)]
impl Composable for Tuple {
for_tuples!( type State = ( #( Tuple::State ),* ); );

fn build(&mut self, cx: &mut BuildContext) -> Self::State {
for_tuples!( ( #( self.Tuple.build(cx) ),* ) )
}

fn rebuild(&mut self, state: &mut Self::State) {
{
for_tuples!(#( self.Tuple.rebuild(&mut state.Tuple); )* )
};
fn compose(&mut self, cx: &mut BuildContext) {
for_tuples!(#( self.Tuple.compose(cx); )*)
}
}
28 changes: 19 additions & 9 deletions src/composition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Composition {
let node = Node {
make_composable,
composable: None,
state: None,

hooks: Rc::default(),
};
let root = composables.insert(node);
Expand Down Expand Up @@ -74,11 +74,10 @@ impl Composition {
nodes: &mut self.nodes,
children: &mut self.children,
};
let state = composable.any_build(&mut build_cx);
let _state = composable.any_build(&mut build_cx);

let node = &mut self.nodes[self.root];
node.composable = Some(composable);
node.state = Some(state);

if let Some(children) = self.children.get(self.root) {
for child_key in children.clone() {
Expand All @@ -100,11 +99,10 @@ impl Composition {
nodes: &mut self.nodes,
children: &mut self.children,
};
let state = composable.any_build(&mut build_cx);
let _state = composable.any_build(&mut build_cx);

let node = &mut self.nodes[child_key];
node.composable = Some(composable);
node.state = Some(state);
}
}

Expand Down Expand Up @@ -150,16 +148,21 @@ impl Composition {
let mut composable = (node.make_composable)();
drop(g);

let mut build_cx = BuildContext {
parent_key: self.root,
nodes: &mut self.nodes,
children: &mut self.children,
};
composable.any_build(&mut build_cx);

let node = &mut self.nodes[self.root];
let state = node.state.as_mut().unwrap();

composable.any_rebuild(&mut **state);
node.composable = Some(composable);

if let Some(children) = self.children.get(self.root) {
for child_key in children.clone() {
let node = &mut self.nodes[child_key];
let state = node.state.as_mut().unwrap();

let cx = LocalContext {
inner: Rc::new(RefCell::new(Inner {
hooks: node.hooks.clone(),
Expand All @@ -172,7 +175,14 @@ impl Composition {
let mut composable = (node.make_composable)();
drop(g);

composable.any_rebuild(&mut **state);
let mut build_cx = BuildContext {
parent_key: child_key,
nodes: &mut self.nodes,
children: &mut self.children,
};
composable.any_build(&mut build_cx);

let node = &mut self.nodes[child_key];
node.composable = Some(composable);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ impl<'a> BuildContext<'a> {
let node = Node {
make_composable,
composable: None,
state: None,
hooks: Rc::default(),
};
let key = self.nodes.insert(node);
Expand Down
1 change: 0 additions & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ use std::{any::Any, cell::RefCell, rc::Rc};
pub struct Node {
pub(crate) make_composable: Box<dyn FnMut() -> Box<dyn AnyComposable>>,
pub(crate) composable: Option<Box<dyn AnyComposable>>,
pub(crate) state: Option<Box<dyn Any>>,
pub(crate) hooks: Rc<RefCell<Vec<Rc<RefCell<dyn Any>>>>>,
}

0 comments on commit 8686777

Please sign in to comment.