add builder methods to NodeMask

This commit is contained in:
Evan Almloff committed 2022-04-16 17:22:03 -05:00
1 parent 436c6a02f7
commit b9c2664089
7 files changed
+45 -172

No files matched your search

-74
View File
@@ -1,74 +0,0 @@
use dioxus_native_core::node_ref::*;
use dioxus_native_core::state::*;
use dioxus_native_core_macro::*;
#[derive(State, Default, Clone)]
#[allow(dead_code)]
struct Z {
// depends on text, the C component of it's parent and a u16 context
#[parent_dep_state(c, u16)]
d: D,
// depends on just attributes and no context
#[node_dep_state()]
a: A,
// depends on the B component of children and i32 context
#[child_dep_state(b, i32)]
b: B,
// depends on the C component of it's parent and a u8 context
#[parent_dep_state(c, u8)]
c: C,
// this will remain uneffected on updates
n: i32,
}
use dioxus_native_core::state::NodeDepState;
#[derive(Default, Clone, Debug)]
struct A;
impl NodeDepState for A {
type Ctx = ();
type DepState = ();
const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::All, false, false, false);
fn reduce(&mut self, _: NodeView, _: &Self::DepState, _: &()) -> bool {
todo!()
}
}
#[derive(Default, Clone, Debug)]
struct B;
impl ChildDepState for B {
type Ctx = i32;
type DepState = Self;
fn reduce<'a>(
&mut self,
_: NodeView,
_: impl Iterator<Item = &'a Self::DepState>,
_: &i32,
) -> bool
where
Self::DepState: 'a,
{
todo!()
}
}
#[derive(Default, Clone, Debug)]
struct C;
impl ParentDepState for C {
type Ctx = u8;
type DepState = Self;
fn reduce(&mut self, _: NodeView, _: Option<&Self::DepState>, _: &u8) -> bool {
todo!()
}
}
#[derive(Default, Clone, Debug)]
struct D;
impl ParentDepState for D {
type Ctx = u16;
type DepState = C;
const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::NONE, false, false, true);
fn reduce(&mut self, _: NodeView, _: Option<&Self::DepState>, _: &u16) -> bool {
todo!()
}
}
-70
View File
@@ -1,70 +0,0 @@
use dioxus_native_core::node_ref::*;
use dioxus_native_core::state::{ChildDepState, NodeDepState, ParentDepState, State};
use dioxus_native_core_macro::State;
#[derive(Debug, Clone, PartialEq, Default)]
struct BubbledUpStateTester(Option<String>, Vec<Box<BubbledUpStateTester>>);
impl ChildDepState for BubbledUpStateTester {
type Ctx = u32;
type DepState = Self;
const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::NONE, true, false, false);
fn reduce<'a>(
&mut self,
node: NodeView,
children: impl Iterator<Item = &'a Self::DepState>,
ctx: &Self::Ctx,
) -> bool
where
Self::DepState: 'a,
{
assert_eq!(*ctx, 42);
*self = BubbledUpStateTester(
node.tag().map(|s| s.to_string()),
children.into_iter().map(|c| Box::new(c.clone())).collect(),
);
true
}
}
#[derive(Debug, Clone, PartialEq, Default)]
struct PushedDownStateTester(Option<String>, Option<Box<PushedDownStateTester>>);
impl ParentDepState for PushedDownStateTester {
type Ctx = u32;
type DepState = Self;
const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::NONE, true, false, false);
fn reduce(&mut self, node: NodeView, parent: Option<&Self::DepState>, ctx: &Self::Ctx) -> bool {
assert_eq!(*ctx, 42);
*self = PushedDownStateTester(
node.tag().map(|s| s.to_string()),
parent.map(|c| Box::new(c.clone())),
);
true
}
}
#[derive(Debug, Clone, PartialEq, Default)]
struct NodeStateTester(Option<String>, Vec<(String, String)>);
impl NodeDepState for NodeStateTester {
type Ctx = u32;
type DepState = ();
const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::All, true, false, false);
fn reduce(&mut self, node: NodeView, _sibling: &Self::DepState, ctx: &Self::Ctx) -> bool {
assert_eq!(*ctx, 42);
*self = NodeStateTester(
node.tag().map(|s| s.to_string()),
node.attributes()
.map(|a| (a.name.to_string(), a.value.to_string()))
.collect(),
);
true
}
}
#[derive(State, Clone, Default, Debug)]
struct StateTester {
#[child_dep_state(bubbled, u32)]
bubbled: BubbledUpStateTester,
#[parent_dep_state(pushed, u32)]
pushed: PushedDownStateTester,
#[node_dep_state(NONE, u32)]
node: NodeStateTester,
}
@@ -72,7 +72,7 @@ struct BubbledUpStateTester(Option<String>, Vec<Box<BubbledUpStateTester>>);
impl ChildDepState for BubbledUpStateTester {
type Ctx = u32;
type DepState = Self;
const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::NONE, true, false, false);
const NODE_MASK: NodeMask = NodeMask::new().with_tag();
fn reduce<'a>(
&mut self,
node: NodeView,
@@ -96,7 +96,7 @@ struct PushedDownStateTester(Option<String>, Option<Box<PushedDownStateTester>>)
impl ParentDepState for PushedDownStateTester {
type Ctx = u32;
type DepState = Self;
const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::NONE, true, false, false);
const NODE_MASK: NodeMask = NodeMask::new().with_tag();
fn reduce(&mut self, node: NodeView, parent: Option<&Self::DepState>, ctx: &Self::Ctx) -> bool {
assert_eq!(*ctx, 42);
*self = PushedDownStateTester(
@@ -112,7 +112,7 @@ struct NodeStateTester(Option<String>, Vec<(String, String)>);
impl NodeDepState for NodeStateTester {
type Ctx = u32;
type DepState = ();
const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::All, true, false, false);
const NODE_MASK: NodeMask = NodeMask::new_with_attrs(AttributeMask::All).with_tag();
fn reduce(&mut self, node: NodeView, _sibling: &Self::DepState, ctx: &Self::Ctx) -> bool {
assert_eq!(*ctx, 42);
*self = NodeStateTester(