more progress on macro

This commit is contained in:
Evan Almloff committed 2022-12-03 22:26:37 -06:00
1 parent 29a61b1855
commit 0e17199e7a
4 files changed
+209 -176

No files matched your search

+51 -33
View File
@@ -3,7 +3,7 @@ extern crate proc_macro;
mod sorted_slice;
use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use quote::{quote, ToTokens, __private::Span};
use sorted_slice::StrSlice;
use syn::parenthesized;
use syn::parse::ParseBuffer;
@@ -148,8 +148,17 @@ fn impl_derive_macro(ast: &syn::DeriveInput) -> TokenStream {
let strct = Struct::new(type_name.clone(), &fields);
match StateStruct::parse(&fields, &strct) {
Ok(state_strct) => {
let member_types1 = state_strct.state_members.iter().map(|m| &m.mem.ty);
let member_types2 = state_strct.state_members.iter().map(|m| &m.mem.ty);
let passes = state_strct.state_members.iter().map(|m| {
let unit = &m.mem.unit_type;
match m.dep_kind {
DependencyKind::Node => quote! {dioxus_native_core::AnyPass::Node(&#unit)},
DependencyKind::Child => quote! {dioxus_native_core::AnyPass::Upward(&#unit)},
DependencyKind::Parent => {
quote! {dioxus_native_core::AnyPass::Downward(&#unit)}
}
}
});
let member_types = state_strct.state_members.iter().map(|m| &m.mem.ty);
let impl_members = state_strct
.state_members
.iter()
@@ -162,9 +171,9 @@ fn impl_derive_macro(ast: &syn::DeriveInput) -> TokenStream {
#(#impl_members)*
impl State for #type_name {
const PASSES: &'static [dioxus_native_core::AnyPass<dioxus_native_core::node::Node<Self>>] = &[
#(dioxus_native_core::AnyPass(&#member_types1)),*
#(#passes),*
];
const MASKS: &'static [dioxus_native_core::NodeMask] = &[#(#member_types2::NODE_MASK),*];
const MASKS: &'static [dioxus_native_core::NodeMask] = &[#(#member_types::NODE_MASK),*];
}
};
gen.into()
@@ -183,7 +192,7 @@ impl Struct {
let members = fields
.iter()
.enumerate()
.filter_map(|(i, f)| Member::parse(f, 1 << i))
.filter_map(|(i, f)| Member::parse(f, i as u64))
.collect();
Self { name, members }
}
@@ -285,6 +294,7 @@ impl Parse for Dependency {
struct Member {
id: u64,
ty: Type,
unit_type: Ident,
ident: Ident,
}
@@ -293,6 +303,11 @@ impl Member {
Some(Self {
id,
ty: field.ty.clone(),
unit_type: Ident::new(
("_Unit".to_string() + field.ty.to_token_stream().to_string().as_str()).as_str(),
Span::call_site(),
)
.into(),
ident: field.ident.as_ref()?.clone(),
})
}
@@ -378,13 +393,14 @@ impl<'a> StateMember<'a> {
};
let ty = &self.mem.ty;
let unit_type = &self.mem.unit_type;
let node_view = quote!(dioxus_native_core::node_ref::NodeView::new(unsafe{&*{&node.node_data as *const _}}, #ty::NODE_MASK));
let dep_idents = self.dep_mems.iter().map(|m| &m.ident);
let impl_specific = match self.dep_kind {
DependencyKind::Node => {
quote! {
impl NodePass<#parent_type> for #ty {
fn pass(&self, node: &mut T, ctx: &SendAnyMap) -> bool {
impl dioxus_native_core::NodePass<dioxus_native_core::node::Node<#parent_type>> for #unit_type {
fn pass(&self, node: &mut dioxus_native_core::node::Node<#parent_type>, ctx: &dioxus_native_core::SendAnyMap) -> bool {
node.state.#ident.reduce(#node_view, (#(&node.state.#dep_idents,)*), #get_ctx)
}
}
@@ -394,12 +410,12 @@ impl<'a> StateMember<'a> {
let update = if self.dep_mems.iter().any(|m| m.id == self.mem.id) {
quote! {
if update {
PassReturn{
dioxus_native_core::PassReturn{
progress: true,
mark_dirty: true,
}
} else {
PassReturn{
dioxus_native_core::PassReturn{
progress: false,
mark_dirty: false,
}
@@ -408,12 +424,12 @@ impl<'a> StateMember<'a> {
} else {
quote! {
if update {
PassReturn{
dioxus_native_core::PassReturn{
progress: false,
mark_dirty: true,
}
} else {
PassReturn{
dioxus_native_core::PassReturn{
progress: false,
mark_dirty: false,
}
@@ -421,13 +437,13 @@ impl<'a> StateMember<'a> {
}
};
quote!(
pub trait UpwardPass<T>: Pass {
impl dioxus_native_core::UpwardPass<dioxus_native_core::node::Node<#parent_type>> for #unit_type{
fn pass<'a>(
&self,
node: &mut T,
children: &mut dyn Iterator<Item = &'a mut T>,
ctx: &SendAnyMap,
) -> PassReturn {
node: &mut dioxus_native_core::node::Node<#parent_type>,
children: &mut dyn Iterator<Item = &'a mut dioxus_native_core::node::Node<#parent_type>>,
ctx: &dioxus_native_core::SendAnyMap,
) -> dioxus_native_core::PassReturn {
let update = node.state.#ident.reduce(#node_view, children.map(|c| (#(&c.state.#dep_idents,)*)), #get_ctx);
#update
}
@@ -438,12 +454,12 @@ impl<'a> StateMember<'a> {
let update = if self.dep_mems.iter().any(|m| m.id == self.mem.id) {
quote! {
if update {
PassReturn{
dioxus_native_core::PassReturn{
progress: true,
mark_dirty: true,
}
} else {
PassReturn{
dioxus_native_core::PassReturn{
progress: false,
mark_dirty: false,
}
@@ -452,12 +468,12 @@ impl<'a> StateMember<'a> {
} else {
quote! {
if update {
PassReturn{
dioxus_native_core::PassReturn{
progress: false,
mark_dirty: true,
}
} else {
PassReturn{
dioxus_native_core::PassReturn{
progress: false,
mark_dirty: false,
}
@@ -465,9 +481,9 @@ impl<'a> StateMember<'a> {
}
};
quote!(
impl DownwardPass<#parent_type> for #ty {
fn pass(&self, node: &mut T, parent: Option<&mut T>, ctx: &SendAnyMap) -> PassReturn{
let update = current_state.state.#ident.reduce(#node_view, parent.as_ref().map(|p| (#(&p.state.#dep_idents,)*)), #get_ctx);
impl dioxus_native_core::DownwardPass<dioxus_native_core::node::Node<#parent_type>> for #unit_type {
fn pass(&self, node: &mut dioxus_native_core::node::Node<#parent_type>, parent: Option<&mut dioxus_native_core::node::Node<#parent_type>>, ctx: &dioxus_native_core::SendAnyMap) -> dioxus_native_core::PassReturn{
let update = node.state.#ident.reduce(#node_view, parent.as_ref().map(|p| (#(&p.state.#dep_idents,)*)), #get_ctx);
#update
}
}
@@ -483,19 +499,21 @@ impl<'a> StateMember<'a> {
.map(|m| m.id)
.fold(self.mem.id, |a, b| a | b);
quote! {
#[derive(Clone, Copy)]
struct #unit_type;
#impl_specific
impl Pass for #ty {
fn pass_id(&self) -> PassId {
PassId(#pass_id)
impl dioxus_native_core::Pass for #unit_type {
fn pass_id(&self) -> dioxus_native_core::PassId {
dioxus_native_core::PassId(#pass_id)
}
fn dependancies(&self) -> &'static [PassId] {
&[#(PassId(#depenancies)),*]
fn dependancies(&self) -> &'static [dioxus_native_core::PassId] {
&[#(dioxus_native_core::PassId(#depenancies)),*]
}
fn dependants(&self) -> &'static [PassId] {
&[#(PassId(#dependants)),*]
fn dependants(&self) -> &'static [dioxus_native_core::PassId] {
&[#(dioxus_native_core::PassId(#dependants)),*]
}
fn mask(&self) -> MemberMask {
MemberMask(#mask)
fn mask(&self) -> dioxus_native_core::MemberMask {
dioxus_native_core::MemberMask(1 << #mask)
}
}
}
@@ -1,161 +1,174 @@
// use anymap::AnyMap;
// use dioxus::prelude::*;
// use dioxus_native_core::node_ref::*;
// use dioxus_native_core::real_dom::*;
// use dioxus_native_core::state::{ChildDepState, NodeDepState, ParentDepState, State};
// use dioxus_native_core_macro::State;
use anymap::AnyMap;
use dioxus::prelude::*;
use dioxus_native_core::node_ref::*;
use dioxus_native_core::real_dom::*;
use dioxus_native_core::state::{
ChildDepState, ElementBorrowable, NodeDepState, ParentDepState, State,
};
use dioxus_native_core::tree::*;
use dioxus_native_core_macro::State;
// macro_rules! dep {
// ( child( $name:ty, $dep:ty ) ) => {
// impl ChildDepState for $name {
// type Ctx = ();
// type DepState = $dep;
// const NODE_MASK: NodeMask = NodeMask::ALL;
// fn reduce<'a>(
// &mut self,
// _: NodeView,
// _: impl Iterator<Item = &'a Self::DepState>,
// _: &Self::Ctx,
// ) -> bool
// where
// Self::DepState: 'a,
// {
// self.0 += 1;
// true
// }
// }
// };
macro_rules! dep {
( child( $name:ty, $dep:ty ) ) => {
impl ChildDepState for $name {
type Ctx = ();
type DepState = $dep;
const NODE_MASK: NodeMask = NodeMask::ALL;
fn reduce<'a>(
&mut self,
_: NodeView,
_: impl Iterator<Item = <Self::DepState as ElementBorrowable>::ElementBorrowed<'a>>,
_: &Self::Ctx,
) -> bool
where
Self::DepState: 'a,
{
self.0 += 1;
true
}
}
};
// ( parent( $name:ty, $dep:ty ) ) => {
// impl ParentDepState for $name {
// type Ctx = ();
// type DepState = $dep;
// const NODE_MASK: NodeMask = NodeMask::ALL;
// fn reduce(&mut self, _: NodeView, _: Option<&Self::DepState>, _: &Self::Ctx) -> bool {
// self.0 += 1;
// true
// }
// }
// };
( parent( $name:ty, $dep:ty ) ) => {
impl ParentDepState for $name {
type Ctx = ();
type DepState = $dep;
const NODE_MASK: NodeMask = NodeMask::ALL;
fn reduce(
&mut self,
_: NodeView,
_: Option<<Self::DepState as ElementBorrowable>::ElementBorrowed<'_>>,
_: &Self::Ctx,
) -> bool {
self.0 += 1;
true
}
}
};
// ( node( $name:ty, $dep:ty ) ) => {
// impl NodeDepState for $name {
// type Ctx = ();
// type DepState<'a> = $dep;
// const NODE_MASK: NodeMask = NodeMask::ALL;
// fn reduce(&mut self, _: NodeView, _: $dep, _: &Self::Ctx) -> bool {
// self.0 += 1;
// true
// }
// }
// };
// }
( node( $name:ty, $dep:ty ) ) => {
impl NodeDepState for $name {
type Ctx = ();
type DepState = $dep;
const NODE_MASK: NodeMask = NodeMask::ALL;
fn reduce(
&mut self,
_: NodeView,
_: <Self::DepState as ElementBorrowable>::ElementBorrowed<'_>,
_: &Self::Ctx,
) -> bool {
self.0 += 1;
true
}
}
};
}
// macro_rules! test_state{
// ( $s:ty, child: ( $( $child:ident ),* ), node: ( $( $node:ident ),* ), parent: ( $( $parent:ident ),* ) ) => {
// #[test]
// fn state_reduce_initally_called_minimally() {
// #[allow(non_snake_case)]
// fn Base(cx: Scope) -> Element {
// render!(div {
// div{
// div{
// p{}
// }
// p{
// "hello"
// }
// div{
// h1{}
// }
// p{
// "world"
// }
// }
// })
// }
macro_rules! test_state{
( $s:ty, child: ( $( $child:ident ),* ), node: ( $( $node:ident ),* ), parent: ( $( $parent:ident ),* ) ) => {
#[test]
fn state_reduce_initally_called_minimally() {
#[allow(non_snake_case)]
fn Base(cx: Scope) -> Element {
render!(div {
div{
div{
p{}
}
p{
"hello"
}
div{
h1{}
}
p{
"world"
}
}
})
}
// let vdom = VirtualDom::new(Base);
let vdom = VirtualDom::new(Base);
// let mutations = vdom.create_vnodes(rsx! {
// div {
// div{
// div{
// p{}
// }
// p{
// "hello"
// }
// div{
// h1{}
// }
// p{
// "world"
// }
// }
// }
// });
let mutations = vdom.create_vnodes(rsx! {
div {
div{
div{
p{}
}
p{
"hello"
}
div{
h1{}
}
p{
"world"
}
}
}
});
// let mut dom: RealDom<$s> = RealDom::new();
let mut dom: RealDom<$s> = RealDom::new();
// let nodes_updated = dom.apply_mutations(vec![mutations]);
// let _to_rerender = dom.update_state(nodes_updated, AnyMap::new());
let nodes_updated = dom.apply_mutations(vec![mutations]);
let _to_rerender = dom.update_state(nodes_updated, AnyMap::new());
// dom.traverse_depth_first(|n| {
// $(
// assert_eq!(n.state.$child.0, 1);
// )*
// $(
// assert_eq!(n.state.$node.0, 1);
// )*
// $(
// assert_eq!(n.state.$parent.0, 1);
// )*
// });
// }
// }
// }
dom.traverse_depth_first(|n| {
$(
assert_eq!(n.state.$child.0, 1);
)*
$(
assert_eq!(n.state.$node.0, 1);
)*
$(
assert_eq!(n.state.$parent.0, 1);
)*
});
}
}
}
// mod node_depends_on_child_and_parent {
// use super::*;
// #[derive(Debug, Clone, Default, PartialEq)]
// struct Node(i32);
// dep!(node(Node, (&'a Child, &'a Parent)));
mod node_depends_on_child_and_parent {
use super::*;
#[derive(Debug, Clone, Default, PartialEq)]
struct Node(i32);
dep!(node(Node, (Child, Parent)));
// #[derive(Debug, Clone, Default, PartialEq)]
// struct Child(i32);
// dep!(child(Child, Child));
#[derive(Debug, Clone, Default, PartialEq)]
struct Child(i32);
dep!(child(Child, (Child,)));
// #[derive(Debug, Clone, Default, PartialEq)]
// struct Parent(i32);
// dep!(parent(Parent, Parent));
#[derive(Debug, Clone, Default, PartialEq)]
struct Parent(i32);
dep!(parent(Parent, (Parent,)));
// #[derive(Debug, Clone, Default, State)]
// struct StateTester {
// #[node_dep_state((child, parent))]
// node: Node,
// #[child_dep_state(child)]
// child: Child,
// #[parent_dep_state(parent)]
// parent: Parent,
// }
#[derive(Debug, Clone, Default, State)]
struct StateTester {
#[node_dep_state((child, parent))]
node: Node,
#[child_dep_state(child)]
child: Child,
#[parent_dep_state(parent)]
parent: Parent,
}
// test_state!(StateTester, child: (child), node: (node), parent: (parent));
// }
// test_state!(StateTester, child: (child), node: (node), parent: (parent));
}
// mod child_depends_on_node_that_depends_on_parent {
// use super::*;
// #[derive(Debug, Clone, Default, PartialEq)]
// struct Node(i32);
// dep!(node(Node, (&'a Parent,)));
// dep!(node(Node, (Parent,)));
// #[derive(Debug, Clone, Default, PartialEq)]
// struct Child(i32);
// dep!(child(Child, Node));
// dep!(child(Child, (Node,)));
// #[derive(Debug, Clone, Default, PartialEq)]
// struct Parent(i32);
// dep!(parent(Parent, Parent));
// dep!(parent(Parent, (Parent,)));
// #[derive(Debug, Clone, Default, State)]
// struct StateTester {
@@ -174,15 +187,15 @@
// use super::*;
// #[derive(Debug, Clone, Default, PartialEq)]
// struct Node(i32);
// dep!(node(Node, (&'a Child,)));
// dep!(node(Node, (Child,)));
// #[derive(Debug, Clone, Default, PartialEq)]
// struct Child(i32);
// dep!(child(Child, Child));
// dep!(child(Child, (Child,)));
// #[derive(Debug, Clone, Default, PartialEq)]
// struct Parent(i32);
// dep!(parent(Parent, Node));
// dep!(parent(Parent, (Node,)));
// #[derive(Debug, Clone, Default, State)]
// struct StateTester {
@@ -201,7 +214,7 @@
// use super::*;
// #[derive(Debug, Clone, Default, PartialEq)]
// struct Node1(i32);
// dep!(node(Node1, (&'a Node2,)));
// dep!(node(Node1, (Node2,)));
// #[derive(Debug, Clone, Default, PartialEq)]
// struct Node2(i32);
@@ -226,11 +239,11 @@
// #[derive(Debug, Clone, Default, PartialEq)]
// struct Child(i32);
// dep!(child(Child, Child));
// dep!(child(Child, (Child,)));
// #[derive(Debug, Clone, Default, PartialEq)]
// struct Parent(i32);
// dep!(parent(Parent, Parent));
// dep!(parent(Parent, (Parent,)));
// #[derive(Debug, Clone, Default, State)]
// struct StateTester {
+4 -2
View File
@@ -1,7 +1,9 @@
use std::hash::BuildHasherDefault;
pub use node_ref::NodeMask;
pub use passes::AnyPass;
pub use passes::{
AnyPass, DownwardPass, MemberMask, NodePass, Pass, PassId, PassReturn, UpwardPass,
};
use rustc_hash::FxHasher;
pub use tree::NodeId;
@@ -18,7 +20,7 @@ pub mod utils;
pub type RealNodeId = NodeId;
pub(crate) type FxDashMap<K, V> = dashmap::DashMap<K, V, BuildHasherDefault<FxHasher>>;
pub(crate) type FxDashSet<K> = dashmap::DashSet<K, BuildHasherDefault<FxHasher>>;
pub(crate) type SendAnyMap = anymap::Map<dyn anymap::any::Any + Send + Sync + 'static>;
pub type SendAnyMap = anymap::Map<dyn anymap::any::Any + Send + Sync + 'static>;
/// Used in derived state macros
#[derive(Eq, PartialEq)]
+2 -2
View File
@@ -111,10 +111,10 @@ impl DirtyNodeStates {
}
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, PartialOrd, Ord)]
pub struct PassId(u64);
pub struct PassId(pub u64);
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Default)]
pub struct MemberMask(u64);
pub struct MemberMask(pub u64);
impl MemberMask {
pub fn overlaps(&self, other: Self) -> bool {