MutationKind

Enum MutationKind 

Source
pub enum MutationKind<T> {
    Replace(T),
    Append(T),
    Truncate(usize),
    Batch(Vec<Mutation<T>>),
}
Expand description

The kind of mutation that occurred.

MutationKind represents the specific type of change made to a value. Different kinds enable optimizations and more precise change descriptions.

§Variants

  • Replace: Complete replacement of a value
  • Append: Append operation for strings and vectors
  • Batch: Multiple mutations combined

§Example

use morphix::adapter::Json;
use morphix::{Mutation, MutationKind, Observe, observe};
use serde::Serialize;
use serde_json::json;

#[derive(Serialize, Observe)]
struct Document {
    title: String,
    content: String,
    tags: Vec<String>,
}

let mut doc = Document {
    title: "Draft".to_string(),
    content: "Hello".to_string(),
    tags: vec!["todo".to_string()],
};

let Json(mutation) = observe!(doc => {
    doc.title = "Final".to_string();      // Replace
    doc.content.push_str(" World");       // Append
    doc.tags.push("done".to_string());    // Append
}).unwrap();

// The mutation contains a Batch with three kinds
assert!(matches!(mutation.unwrap().kind, MutationKind::Batch(_)));

Variants§

§

Replace(T)

Replace is the default mutation for DerefMut operations.

§Examples
foo.a.b = 1;        // Replace at .a.b
foo.num *= 2;       // Replace at .num
foo.vec.clear();    // Replace at .vec
§Note

If an operation can be represented as Append, it will be preferred over Replace for efficiency.

§

Append(T)

Available on crate feature append only.

Append represents adding data to the end of a string or vector. This is more efficient than Replace because only the appended portion needs to be serialized and transmitted.

§Examples
foo.a.b += "text";          // Append to .a.b
foo.a.b.push_str("text");   // Append to .a.b
foo.vec.push(1);            // Append to .vec
foo.vec.extend(iter);       // Append to .vec
§

Truncate(usize)

Available on crate feature truncate only.

Truncate represents removing elements from the end of a string or vector. This is more efficient than Replace because only the truncation length needs to be serialized and transmitted.

§Examples
let mut foo = Foo {
    a: A { b: "Hello, World!".to_string() },
    vec: vec![1, 2, 3, 4, 5],
};
foo.a.b.truncate(5);        // Truncate 8 chars from .a.b
foo.vec.truncate(2);        // Truncate 3 elements from .vec
§

Batch(Vec<Mutation<T>>)

Batch combines multiple mutations that occurred during a single observation period. This is automatically created when multiple independent changes are detected.

§Optimization

The batch collector (BatchTree) automatically optimizes mutations:

  • Consecutive appends are merged
  • Redundant changes are eliminated
  • Nested paths are consolidated when possible

Trait Implementations§

Source§

impl<T: Clone> Clone for MutationKind<T>

Source§

fn clone(&self) -> MutationKind<T>

Returns a duplicate 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<T: Debug> Debug for MutationKind<T>

Source§

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

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

impl<T: PartialEq> PartialEq for MutationKind<T>

Source§

fn eq(&self, other: &MutationKind<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq> Eq for MutationKind<T>

Source§

impl<T> StructuralPartialEq for MutationKind<T>

Auto Trait Implementations§

§

impl<T> Freeze for MutationKind<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for MutationKind<T>
where T: RefUnwindSafe,

§

impl<T> Send for MutationKind<T>
where T: Send,

§

impl<T> Sync for MutationKind<T>
where T: Sync,

§

impl<T> Unpin for MutationKind<T>
where T: Unpin,

§

impl<T> UnwindSafe for MutationKind<T>
where T: UnwindSafe,

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> AsDeref<Zero> for T
where T: ?Sized,

Source§

type Target = T

The target type after N dereferences.
Source§

fn as_deref(&self) -> &T

Dereferences self N times.
Source§

impl<T> AsDerefCoinductive<Zero> for T
where T: ?Sized,

Source§

type Target = T

The target type after N dereferences.
Source§

fn as_deref_coinductive(&self) -> &T

Dereferences self N times.
Source§

impl<T> AsDerefMut<Zero> for T
where T: ?Sized,

Source§

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

Mutably dereferences self N times.
Source§

impl<T> AsDerefMutCoinductive<Zero> for T
where T: ?Sized,

Source§

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

Mutably dereferences self N times.
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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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

Source§

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

Source§

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.