Struct sauron::vdom::Patch

source ·
pub struct Patch<'a, MSG> {
    pub tag: Option<&'a &'static str>,
    pub patch_path: TreePath,
    pub patch_type: PatchType<'a, MSG>,
}
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 &'static str>

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, MSG>

the type of patch we are going to apply

Implementations§

source§

impl<'a, MSG> Patch<'a, MSG>

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<&&'static str>

return the tag of this patch

source

pub fn insert_before_node( tag: Option<&'a &'static str>, patch_path: TreePath, nodes: impl IntoIterator<Item = &'a Node<MSG>> ) -> Patch<'a, MSG>

create an InsertBeforeNode patch

source

pub fn insert_after_node( tag: Option<&'a &'static str>, patch_path: TreePath, nodes: Vec<&'a Node<MSG>> ) -> Patch<'a, MSG>

create an InsertAfterNode patch

source

pub fn append_children( tag: Option<&'a &'static str>, patch_path: TreePath, children: Vec<&'a Node<MSG>> ) -> Patch<'a, MSG>

create a patch where we add children to the target node

source

pub fn remove_node( tag: Option<&'a &'static str>, patch_path: TreePath ) -> Patch<'a, MSG>

create a patch where the target element that can be traverse using the patch path will be remove

source

pub fn clear_children( tag: Option<&'a &'static str>, patch_path: TreePath ) -> Patch<'a, MSG>

create a patch where the target element has to clear its children nodes

source

pub fn move_before_node( tag: Option<&'a &'static str>, patch_path: TreePath, nodes_path: impl IntoIterator<Item = TreePath> ) -> Patch<'a, MSG>

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 &'static str>, patch_path: TreePath, nodes_path: impl IntoIterator<Item = TreePath> ) -> Patch<'a, MSG>

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 &'static str>, patch_path: TreePath, replacement: impl IntoIterator<Item = &'a Node<MSG>> ) -> Patch<'a, MSG>

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 &'static str, patch_path: TreePath, attrs: impl IntoIterator<Item = &'a Attribute<MSG>> ) -> Patch<'a, MSG>

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

source

pub fn remove_attributes( tag: &'a &'static str, patch_path: TreePath, attrs: Vec<&'a Attribute<MSG>> ) -> Patch<'a, MSG>

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

source

pub fn map_msg<F, MSG2>(self, cb: F) -> Patch<'a, MSG2>
where F: Fn(MSG) -> MSG2 + Clone + 'static, MSG2: 'static, MSG: 'static,

map the msg of this patch such that Patch<MSG> becomes Patch<MSG2>

Trait Implementations§

source§

impl<'a, MSG> Clone for Patch<'a, MSG>

source§

fn clone(&self) -> Patch<'a, MSG>

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, MSG> Debug for Patch<'a, MSG>

source§

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

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

impl<'a, MSG> PartialEq for Patch<'a, MSG>

source§

fn eq(&self, __other: &Patch<'a, MSG>) -> 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, MSG> Eq for Patch<'a, MSG>

Auto Trait Implementations§

§

impl<'a, MSG> Freeze for Patch<'a, MSG>

§

impl<'a, MSG> !RefUnwindSafe for Patch<'a, MSG>

§

impl<'a, MSG> !Send for Patch<'a, MSG>

§

impl<'a, MSG> !Sync for Patch<'a, MSG>

§

impl<'a, MSG> Unpin for Patch<'a, MSG>

§

impl<'a, MSG> !UnwindSafe for Patch<'a, MSG>

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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.