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 valueAppend: Append operation for strings and vectorsBatch: 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)
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 .vecTruncate(usize)
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 .vecBatch(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>
impl<T: Clone> Clone for MutationKind<T>
Source§fn clone(&self) -> MutationKind<T>
fn clone(&self) -> MutationKind<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug> Debug for MutationKind<T>
impl<T: Debug> Debug for MutationKind<T>
Source§impl<T: PartialEq> PartialEq for MutationKind<T>
impl<T: PartialEq> PartialEq for MutationKind<T>
impl<T: Eq> Eq for MutationKind<T>
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> AsDerefCoinductive<Zero> for Twhere
T: ?Sized,
impl<T> AsDerefCoinductive<Zero> for Twhere
T: ?Sized,
Source§impl<T> AsDerefMut<Zero> for Twhere
T: ?Sized,
impl<T> AsDerefMut<Zero> for Twhere
T: ?Sized,
Source§fn as_deref_mut(&mut self) -> &mut T
fn as_deref_mut(&mut self) -> &mut T
N times.Source§impl<T> AsDerefMutCoinductive<Zero> for Twhere
T: ?Sized,
impl<T> AsDerefMutCoinductive<Zero> for Twhere
T: ?Sized,
Source§fn as_deref_mut_coinductive(&mut self) -> &mut T
fn as_deref_mut_coinductive(&mut self) -> &mut T
N times.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.