Struct mt_dom::patch::Patch

source ·
pub struct 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,
{ pub tag: Option<&'a Tag>, pub patch_path: TreePath, pub patch_type: PatchType<'a, Ns, Tag, Leaf, 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.

Fields§

§tag: Option<&'a Tag>

the tag of the node at patch_path

§patch_path: TreePath

the path to traverse to get to the target element

§patch_type: PatchType<'a, Ns, Tag, Leaf, Att, Val>

the type of patch we are going to apply

Implementations§

source§

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,

source

pub fn path(&self) -> &TreePath

return the path to traverse for this patch to get to the target Node

source

pub fn node_paths(&self) -> &[TreePath]

return the node paths involve such as those in moving nodes

source

pub fn tag(&self) -> Option<&Tag>

return the tag of this patch

source

pub fn insert_before_node( tag: Option<&'a Tag>, patch_path: TreePath, nodes: impl IntoIterator<Item = &'a Node<Ns, Tag, Leaf, Att, Val>> ) -> Patch<'a, Ns, Tag, Leaf, Att, Val>

create an InsertBeforeNode patch

source

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

source

pub fn append_children( tag: Option<&'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

source

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

source

pub fn move_before_node( tag: Option<&'a Tag>, patch_path: TreePath, nodes_path: impl IntoIterator<Item = TreePath> ) -> Patch<'a, Ns, Tag, Leaf, Att, Val>

remove the nodes pointed at the nodes_path and insert them before the target element pointed at patch_path

source

pub fn move_after_node( tag: Option<&'a Tag>, patch_path: TreePath, nodes_path: impl IntoIterator<Item = TreePath> ) -> Patch<'a, Ns, Tag, Leaf, Att, Val>

remove the nodes pointed at the nodes_path and insert them after the target element pointed at patch_path

source

pub fn replace_node( tag: Option<&'a Tag>, patch_path: TreePath, replacement: impl IntoIterator<Item = &'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

source

pub fn add_attributes( tag: &'a Tag, patch_path: TreePath, attrs: impl IntoIterator<Item = &'a Attribute<Ns, Att, Val>> ) -> Patch<'a, Ns, Tag, Leaf, Att, Val>

create a patch where a new attribute is added to the target element

source

pub fn remove_attributes( tag: &'a Tag, patch_path: TreePath, attrs: Vec<&'a Attribute<Ns, Att, Val>> ) -> Patch<'a, Ns, Tag, Leaf, Att, Val>

create patch where it remove attributes of the target element that can be traversed by the patch_path.

Trait Implementations§

source§

impl<'a, Ns, Tag, Leaf, Att, Val> Clone for Patch<'a, Ns, Tag, Leaf, Att, Val>
where Ns: PartialEq + Clone + Debug + Clone, Tag: PartialEq + Debug + Clone, Leaf: PartialEq + Clone + Debug + Clone, Att: PartialEq + Clone + Debug + Clone, Val: PartialEq + Clone + Debug + Clone,

source§

fn clone(&self) -> Patch<'a, Ns, Tag, Leaf, Att, Val>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a, Ns, Tag, Leaf, Att, Val> Debug for Patch<'a, Ns, Tag, Leaf, Att, Val>
where Ns: PartialEq + Clone + Debug + Debug, Tag: PartialEq + Debug + Debug, Leaf: PartialEq + Clone + Debug + Debug, Att: PartialEq + Clone + Debug + Debug, Val: PartialEq + Clone + Debug + Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, Ns, Tag, Leaf, Att, Val> PartialEq for Patch<'a, Ns, Tag, Leaf, Att, Val>

source§

fn eq(&self, other: &Patch<'a, Ns, Tag, Leaf, Att, Val>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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>

§

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>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.