pub enum MutationKind<A: Adapter> {
Replace(A::Value),
Append(A::Value),
Batch(Vec<Mutation<A>>),
}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::{JsonAdapter, 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 mutation = observe!(JsonAdapter, |mut doc| {
doc.title = "Final".to_string(); // Replace
doc.content.push_str(" World"); // Append
doc.tags.push("done".to_string()); // Append
}).unwrap().unwrap();
// The mutation contains a Batch with three operations
matches!(mutation.operation, MutationKind::Batch(_));Variants§
Replace(A::Value)
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(A::Value)
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 .vecBatch(Vec<Mutation<A>>)
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 (Batch) automatically optimizes mutations:
- Consecutive appends are merged
- Redundant changes are eliminated
- Nested paths are consolidated when possible
Trait Implementations§
Source§impl<A: Adapter> Clone for MutationKind<A>
impl<A: Adapter> Clone for MutationKind<A>
Source§impl<A: Adapter> Debug for MutationKind<A>
impl<A: Adapter> Debug for MutationKind<A>
Source§impl<A: Adapter> PartialEq for MutationKind<A>
impl<A: Adapter> PartialEq for MutationKind<A>
impl<A: Adapter> Eq for MutationKind<A>
Auto Trait Implementations§
impl<A> Freeze for MutationKind<A>
impl<A> RefUnwindSafe for MutationKind<A>
impl<A> Send for MutationKind<A>
impl<A> Sync for MutationKind<A>
impl<A> Unpin for MutationKind<A>
impl<A> UnwindSafe for MutationKind<A>
Blanket Implementations§
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
Mutably borrows from an owned value. Read more