pub enum Patch<'a, NS, TAG, LEAF, ATT, VAL> where
NS: PartialEq + Clone + Debug,
TAG: PartialEq + Debug,
LEAF: PartialEq + Clone + Debug,
ATT: PartialEq + Clone + Debug,
VAL: PartialEq + Clone + Debug, {
InsertBeforeNode {
tag: Option<&'a TAG>,
patch_path: TreePath,
nodes: Vec<&'a Node<NS, TAG, LEAF, ATT, VAL>>,
},
InsertAfterNode {
tag: Option<&'a TAG>,
patch_path: TreePath,
nodes: Vec<&'a Node<NS, TAG, LEAF, ATT, VAL>>,
},
AppendChildren {
tag: &'a TAG,
patch_path: TreePath,
children: Vec<&'a Node<NS, TAG, LEAF, ATT, VAL>>,
},
RemoveNode {
tag: Option<&'a TAG>,
patch_path: TreePath,
},
ReplaceNode {
tag: Option<&'a TAG>,
patch_path: TreePath,
replacement: &'a Node<NS, TAG, LEAF, ATT, VAL>,
},
AddAttributes {
tag: &'a TAG,
patch_path: TreePath,
attrs: Vec<&'a Attribute<NS, ATT, VAL>>,
},
RemoveAttributes {
tag: &'a TAG,
patch_path: TreePath,
attrs: Vec<&'a Attribute<NS, ATT, VAL>>,
},
}
Expand description
A Patch encodes an operation that modifies a real DOM element or native UI element
To update the real DOM that a user sees you’ll want to first diff your old virtual dom and new virtual dom.
This diff operation will generate Vec<Patch>
with zero or more patches that, when
applied to your real DOM, will make your real DOM look like your new virtual dom.
Each of the Patch contains TreePath
which contains an array of indexes for each node
that we need to traverse to get the target element.
Consider the following html:
<body>
<main>
<input type="text"/>
<img src="pic.jpg"/>
</main>
<footer>
<a>Link</a>
<nav/>
</footer>
</body>
The corresponding DOM tree would be
.─.
( 0 ) <body>
`-'
/ \
/ \
/ \
▼ ▼
<main> .─. .─. <footer>
( 0 ) ( 1 )
`-' `-'
/ \ | \ '.
/ \ | \ '.
▼ ▼ | \ '.
.─. .─. ▼ ▼ ▼
( 0 ) ( 1 ) .─. .─. .─.
`─' `─' ( 0 ) ( 1 ) ( 2 )
<input> <img> `─' `─' `─'
<a> <Text> <nav>
To traverse to the <nav>
element we follow the TreePath([0,1,2]).
0 - is the root element which is always zero.
1 - is the footer
element since it is the 2nd element of the body.
2 - is the nav
element since it is the 3rd node in the footer
element.
Variants
InsertBeforeNode
Fields
patch_path: TreePath
the path to traverse to get to the target element of which our nodes will be inserted before it.
insert the nodes before the node at patch_path
InsertAfterNode
Fields
patch_path: TreePath
the path to traverse to get to the target element of which our nodes will be inserted after it.
insert the nodes after the node at patch_path
AppendChildren
Fields
tag: &'a TAG
the tag of the node we are appending the children into
patch_path: TreePath
index of the node we are going to append the children into
Append a vector of child nodes to a parent node id at patch_path
RemoveNode
Fields
tag: Option<&'a TAG>
The tag of the node that is to be removed. This is only used for additional check where are removing the correct node.
patch_path: TreePath
the node_idx of the node to be removed
remove node
ReplaceNode
Fields
tag: Option<&'a TAG>
The tag of the node we are going to replace. This is only used for additional checking that we are removing the correct node.
patch_path: TreePath
the traversal path of the node we are going to replace
replacement: &'a Node<NS, TAG, LEAF, ATT, VAL>
the node that will replace the target node
ReplaceNode a node with another node. This typically happens when a node’s tag changes. ex:
AddAttributes
Fields
tag: &'a TAG
node tag use for verifying that the we are patching the correct node which should match the same tag
patch_path: TreePath
the path to traverse to get to the target lement of which we add the attributes.
Add attributes that the new node has that the old node does not Note: the attributes is not a reference since attributes of same name are merged to produce a new unify attribute
RemoveAttributes
Fields
tag: &'a TAG
The tag of the node we are removing the attributes from. This is only used for additional check that we are patching the correct node
patch_path: TreePath
the path to traverse to get to the target lement of which we remove the attributes
Remove attributes that the old node had that the new node doesn’t
Implementations
sourceimpl<'a, NS, TAG, LEAF, ATT, VAL> Patch<'a, NS, TAG, LEAF, ATT, VAL> where
NS: PartialEq + Clone + Debug,
TAG: PartialEq + Debug,
LEAF: PartialEq + Clone + Debug,
ATT: PartialEq + Clone + Debug,
VAL: PartialEq + Clone + Debug,
impl<'a, NS, TAG, LEAF, ATT, VAL> Patch<'a, NS, TAG, LEAF, ATT, VAL> where
NS: PartialEq + Clone + Debug,
TAG: PartialEq + Debug,
LEAF: PartialEq + Clone + Debug,
ATT: PartialEq + Clone + Debug,
VAL: PartialEq + Clone + Debug,
sourcepub fn path(&self) -> &TreePath
pub fn path(&self) -> &TreePath
return the path to traverse for this patch to get to the target Node
sourcepub fn insert_node(
tag: Option<&'a TAG>,
patch_path: TreePath,
node: &'a Node<NS, TAG, LEAF, ATT, VAL>
) -> Patch<'a, NS, TAG, LEAF, ATT, VAL>
pub fn insert_node(
tag: Option<&'a TAG>,
patch_path: TreePath,
node: &'a Node<NS, TAG, LEAF, ATT, VAL>
) -> Patch<'a, NS, TAG, LEAF, ATT, VAL>
TODO: remove this one create an InsertNode patch
sourcepub fn insert_before_node(
tag: Option<&'a TAG>,
patch_path: TreePath,
nodes: Vec<&'a Node<NS, TAG, LEAF, ATT, VAL>>
) -> Patch<'a, NS, TAG, LEAF, ATT, VAL>
pub fn insert_before_node(
tag: Option<&'a TAG>,
patch_path: TreePath,
nodes: Vec<&'a Node<NS, TAG, LEAF, ATT, VAL>>
) -> Patch<'a, NS, TAG, LEAF, ATT, VAL>
create an InsertBeforeNode patch
sourcepub fn insert_after_node(
tag: Option<&'a TAG>,
patch_path: TreePath,
nodes: Vec<&'a Node<NS, TAG, LEAF, ATT, VAL>>
) -> Patch<'a, NS, TAG, LEAF, ATT, VAL>
pub fn insert_after_node(
tag: Option<&'a TAG>,
patch_path: TreePath,
nodes: Vec<&'a Node<NS, TAG, LEAF, ATT, VAL>>
) -> Patch<'a, NS, TAG, LEAF, ATT, VAL>
create an InsertAfterNode patch
sourcepub fn append_children(
tag: &'a TAG,
patch_path: TreePath,
children: Vec<&'a Node<NS, TAG, LEAF, ATT, VAL>>
) -> Patch<'a, NS, TAG, LEAF, ATT, VAL>
pub fn append_children(
tag: &'a TAG,
patch_path: TreePath,
children: Vec<&'a Node<NS, TAG, LEAF, ATT, VAL>>
) -> Patch<'a, NS, TAG, LEAF, ATT, VAL>
create a patch where we add children to the target node
sourcepub fn remove_node(
tag: Option<&'a TAG>,
patch_path: TreePath
) -> Patch<'a, NS, TAG, LEAF, ATT, VAL>
pub fn remove_node(
tag: Option<&'a TAG>,
patch_path: TreePath
) -> Patch<'a, NS, TAG, LEAF, ATT, VAL>
create a patch where the target element that can be traverse using the patch path will be remove
sourcepub fn replace_node(
tag: Option<&'a TAG>,
patch_path: TreePath,
replacement: &'a Node<NS, TAG, LEAF, ATT, VAL>
) -> Patch<'a, NS, TAG, LEAF, ATT, VAL>
pub fn replace_node(
tag: Option<&'a TAG>,
patch_path: TreePath,
replacement: &'a Node<NS, TAG, LEAF, ATT, VAL>
) -> Patch<'a, NS, TAG, LEAF, ATT, VAL>
create a patch where a node is replaced by the replacement
node.
The target node to be replace is traverse using the patch_path
Trait Implementations
sourceimpl<'a, NS: Clone, TAG: Clone, LEAF: Clone, ATT: Clone, VAL: Clone> Clone for Patch<'a, NS, TAG, LEAF, ATT, VAL> where
NS: PartialEq + Clone + Debug,
TAG: PartialEq + Debug,
LEAF: PartialEq + Clone + Debug,
ATT: PartialEq + Clone + Debug,
VAL: PartialEq + Clone + Debug,
impl<'a, NS: Clone, TAG: Clone, LEAF: Clone, ATT: Clone, VAL: Clone> Clone for Patch<'a, NS, TAG, LEAF, ATT, VAL> where
NS: PartialEq + Clone + Debug,
TAG: PartialEq + Debug,
LEAF: PartialEq + Clone + Debug,
ATT: PartialEq + Clone + Debug,
VAL: PartialEq + Clone + Debug,
sourceimpl<'a, NS: Debug, TAG: Debug, LEAF: Debug, ATT: Debug, VAL: Debug> Debug for Patch<'a, NS, TAG, LEAF, ATT, VAL> where
NS: PartialEq + Clone + Debug,
TAG: PartialEq + Debug,
LEAF: PartialEq + Clone + Debug,
ATT: PartialEq + Clone + Debug,
VAL: PartialEq + Clone + Debug,
impl<'a, NS: Debug, TAG: Debug, LEAF: Debug, ATT: Debug, VAL: Debug> Debug for Patch<'a, NS, TAG, LEAF, ATT, VAL> where
NS: PartialEq + Clone + Debug,
TAG: PartialEq + Debug,
LEAF: PartialEq + Clone + Debug,
ATT: PartialEq + Clone + Debug,
VAL: PartialEq + Clone + Debug,
sourceimpl<'a, NS: PartialEq, TAG: PartialEq, LEAF: PartialEq, ATT: PartialEq, VAL: PartialEq> PartialEq<Patch<'a, NS, TAG, LEAF, ATT, VAL>> for Patch<'a, NS, TAG, LEAF, ATT, VAL> where
NS: PartialEq + Clone + Debug,
TAG: PartialEq + Debug,
LEAF: PartialEq + Clone + Debug,
ATT: PartialEq + Clone + Debug,
VAL: PartialEq + Clone + Debug,
impl<'a, NS: PartialEq, TAG: PartialEq, LEAF: PartialEq, ATT: PartialEq, VAL: PartialEq> PartialEq<Patch<'a, NS, TAG, LEAF, ATT, VAL>> for Patch<'a, NS, TAG, LEAF, ATT, VAL> where
NS: PartialEq + Clone + Debug,
TAG: PartialEq + Debug,
LEAF: PartialEq + Clone + Debug,
ATT: PartialEq + Clone + Debug,
VAL: PartialEq + Clone + Debug,
impl<'a, NS, TAG, LEAF, ATT, VAL> StructuralPartialEq for Patch<'a, NS, TAG, LEAF, ATT, VAL> where
NS: PartialEq + Clone + Debug,
TAG: PartialEq + Debug,
LEAF: PartialEq + Clone + Debug,
ATT: PartialEq + Clone + Debug,
VAL: PartialEq + Clone + Debug,
Auto Trait Implementations
impl<'a, NS, TAG, LEAF, ATT, VAL> RefUnwindSafe for Patch<'a, NS, TAG, LEAF, ATT, VAL> where
ATT: RefUnwindSafe,
LEAF: RefUnwindSafe,
NS: RefUnwindSafe,
TAG: RefUnwindSafe,
VAL: RefUnwindSafe,
impl<'a, NS, TAG, LEAF, ATT, VAL> Send for Patch<'a, NS, TAG, LEAF, ATT, VAL> where
ATT: Sync,
LEAF: Sync,
NS: Sync,
TAG: Sync,
VAL: Sync,
impl<'a, NS, TAG, LEAF, ATT, VAL> Sync for Patch<'a, NS, TAG, LEAF, ATT, VAL> where
ATT: Sync,
LEAF: Sync,
NS: Sync,
TAG: Sync,
VAL: Sync,
impl<'a, NS, TAG, LEAF, ATT, VAL> Unpin for Patch<'a, NS, TAG, LEAF, ATT, VAL>
impl<'a, NS, TAG, LEAF, ATT, VAL> UnwindSafe for Patch<'a, NS, TAG, LEAF, ATT, VAL> where
ATT: RefUnwindSafe,
LEAF: RefUnwindSafe,
NS: RefUnwindSafe,
TAG: RefUnwindSafe,
VAL: RefUnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more