pub struct Patch { /* private fields */ }Expand description
A collection of operations to apply atomically.
Patches are the primary way to describe changes to a document. Operations are applied in order.
§Examples
use tirea_state::{Patch, Op, path};
use serde_json::json;
let patch = Patch::new()
.with_op(Op::set(path!("name"), json!("Alice")))
.with_op(Op::set(path!("age"), json!(30)));
assert_eq!(patch.len(), 2);Implementations§
Source§impl Patch
impl Patch
Sourcepub fn canonicalize(&self) -> Patch
pub fn canonicalize(&self) -> Patch
Canonicalize the patch by removing redundant operations.
This is a safe, semantics-preserving optimization that:
- Removes earlier
Setoperations when a laterSettargets the same path - Removes earlier
Setoperations when a laterDeletetargets the same path - Removes earlier
Deleteoperations when a laterSetorDeletetargets the same path
Important: This function only removes redundant operations. It never reorders the remaining operations. The output is always a subsequence of the input in the same relative order.
Operations that are not optimized (kept as-is):
Increment,Decrement,MergeObject: These have cumulative effects and cannot be safely deduplicated without semantic analysisAppend,Insert,Remove: Array operations are order-sensitive
§Examples
use tirea_state::{Patch, Op, path};
use serde_json::json;
let patch = Patch::new()
.with_op(Op::set(path!("name"), json!("Alice")))
.with_op(Op::set(path!("name"), json!("Bob"))); // overwrites Alice
let canonical = patch.canonicalize();
assert_eq!(canonical.len(), 1);
assert_eq!(canonical.ops()[0], Op::set(path!("name"), json!("Bob")));Trait Implementations§
Source§impl<'de> Deserialize<'de> for Patch
impl<'de> Deserialize<'de> for Patch
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl Extend<Op> for Patch
impl Extend<Op> for Patch
Source§fn extend<I: IntoIterator<Item = Op>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = Op>>(&mut self, iter: I)
Extends a collection with the contents of an iterator. Read more
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
🔬This is a nightly-only experimental API. (
extend_one)Extends a collection with exactly one element.
Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one)Reserves capacity in a collection for the given number of additional elements. Read more
Source§impl From<Patch> for TrackedPatch
impl From<Patch> for TrackedPatch
Source§impl FromIterator<Op> for Patch
impl FromIterator<Op> for Patch
Source§impl<'a> IntoIterator for &'a Patch
impl<'a> IntoIterator for &'a Patch
Source§impl IntoIterator for Patch
impl IntoIterator for Patch
impl StructuralPartialEq for Patch
Auto Trait Implementations§
impl Freeze for Patch
impl RefUnwindSafe for Patch
impl Send for Patch
impl Sync for Patch
impl Unpin for Patch
impl UnwindSafe for Patch
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