Skip to content

Commit

Permalink
Create OneOfN views
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Jan 20, 2024
1 parent 4af1c5c commit e88d579
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
1 change: 0 additions & 1 deletion crates/concoct/src/hook/use_effect.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::use_ref;
use rustc_hash::FxHasher;

use std::hash::{Hash, Hasher};

pub fn use_effect(dependencies: impl Hash, effect: impl FnOnce()) {
Expand Down
60 changes: 60 additions & 0 deletions crates/concoct/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,66 @@ impl<T: Tree> Tree for Option<T> {
}
}

macro_rules! one_of {
($name:tt, $($t:tt),*) => {
pub enum $name<$($t),*> {
$($t($t)),*
}

impl<$($t: Body),*> Body for $name<$($t),*> {
fn into_tree(self) -> impl Tree {
match self {
$(
$name::$t(body) => $name::$t(body.into_tree()),
)*
}
}
}

impl<$($t: Tree),*> Tree for $name<$($t),*> {
fn build(&mut self) {
match self {
$(
$name::$t(tree) => tree.build(),
)*
}
}

fn rebuild(&mut self, last: &mut dyn Any) {
let last = last.downcast_mut::<Self>().unwrap();
match (self, last) {
$(
($name::$t(tree), $name::$t(last_tree)) => {
tree.rebuild(last_tree)
}
),*
(me, last) => {
last.remove();
me.build();
}
}

}

fn remove(&mut self) {
match self {
$(
$name::$t(tree) => tree.remove(),
)*
}
}
}
};
}

one_of!(OneOf2, A, B);
one_of!(OneOf3, A, B, C);
one_of!(OneOf4, A, B, C, D);
one_of!(OneOf5, A, B, C, D, E);
one_of!(OneOf6, A, B, C, D, E, F);
one_of!(OneOf7, A, B, C, D, E, F, G);
one_of!(OneOf8, A, B, C, D, E, F, G, H);

macro_rules! impl_tree_for_tuple {
($($t:tt : $idx:tt),*) => {
impl<$($t: Tree),*> Tree for ($($t),*) {
Expand Down

0 comments on commit e88d579

Please sign in to comment.