Skip to main content

Mutations

Struct Mutations 

Source
pub struct Mutations<V = Box<dyn Serialize>> { /* private fields */ }
Expand description

A collection of mutations collected during observation.

It is the return type for flush and flat_flush operations.

§Behavior

  • If no mutations are pushed, into_inner returns None.
  • If exactly one mutation is pushed, it is returned as-is.
  • If multiple mutations are pushed, they are wrapped in a Batch.

The is_replace flag tracks whether this collection represents a whole-value replace. It is set automatically when constructed from a Replace mutation, and is used by flat_flush to propagate replace status to parent observers for replace collapse.

§Example

use morphix::{Mutation, MutationKind, Mutations};

let mut mutations = Mutations::new();

mutations.insert("a", MutationKind::Replace(42));
mutations.insert("b", MutationKind::Truncate(1));

let result = mutations.into_inner();
assert!(matches!(result, Some(Mutation { kind: MutationKind::Batch(_), .. })));

Implementations§

Source§

impl<V> Mutations<V>

Source

pub fn new() -> Self

Creates a new empty collection.

Source

pub fn with_capacity(self, capacity: usize) -> Self

Sets the capacity hint for the internal Batch storage.

The capacity hint is used when the internal storage needs to be converted to a Batch to hold multiple mutations.

Source

pub fn with_replace(self, is_replace: bool) -> Self

Sets the is_replace flag on this collection.

This is used by composite observers to propagate replace status through flat_flush, where the collection may contain per-field mutations that are a flattened decomposition of a whole-value replace.

Source

pub fn with_prefix(self, segment: impl Into<PathSegment>) -> Self

Prepends a path segment to the contained mutation.

If the collection is empty, this is a no-op. Otherwise, the segment is pushed onto the mutation’s reverse-order path.

Source

pub fn into_inner(self) -> Option<Mutation<V>>

Consumes the batch and returns the collected mutation.

Source

pub fn is_replace(&self) -> bool

Returns true if this collection represents a whole-value replace.

This flag is set automatically when the collection is created from a Replace mutation kind, or explicitly via with_replace. It is used by flat_flush to signal to the parent observer that the entire content was replaced, enabling replace collapse.

Source

pub fn extend(&mut self, mutations: impl Into<Self>)

Merges another collection of mutations into this one.

If the incoming collection contains a Batch with an empty path, its inner mutations are flattened into this collection rather than being nested.

Source

pub fn insert( &mut self, segment: impl Into<PathSegment>, mutations: impl Into<Self>, )

Inserts mutations at a specified path segment.

The incoming mutations will have the given segment prepended to their path before being added to this collection.

Source

pub fn len(&self) -> usize

Returns the number of top-level mutations in this collection.

A top-level mutation is one with an empty path. If this collection contains a Batch with an empty path, this returns the number of mutations in that batch. Otherwise, it returns 1 if a mutation exists, or 0 if the collection is empty.

Source

pub fn is_empty(&self) -> bool

Returns true if this collection contains no mutations.

Source

pub fn truncate(len: usize) -> Self

Available on crate feature truncate only.

Creates a Mutations containing a single Truncate mutation.

Source

pub fn delete() -> Self

Available on crate feature delete only.

Creates a Mutations containing a single Delete mutation.

Source

pub fn into_delete(self) -> Self

Available on crate feature delete only.

Converts all mutations in this collection to Delete.

Each mutation retains its path but has its kind replaced with Delete. For a Batch, every inner mutation is converted individually, preserving the per-field paths. This is used by flat_flush when the parent needs to emit deletions for all fields of a flattened struct or map.

Source§

impl Mutations

Source

pub fn replace_owned<T: Serialize + 'static>(value: T) -> Self

Creates a Mutations containing a single Replace mutation, taking ownership of the value.

Unlike replace, which accepts &T (including unsized types) and wraps it in [SerializeRef], this method takes T by value and boxes it directly.

Source

pub fn replace<T: Serialize + ?Sized + 'static>(value: &T) -> Self

Creates a Mutations containing a single Replace mutation with the given value.

The value is wrapped in a Box<dyn Serialize> via [SerializeRef], allowing unsized types like str and [T] to be used.

Source

pub fn append_owned<T: Serialize + 'static>(value: T) -> Self

Available on crate feature append only.

Creates a Mutations containing a single Append mutation, taking ownership of the value.

Unlike append, which accepts &T (including unsized types) and wraps it in [SerializeRef], this method takes T by value and boxes it directly.

Source

pub fn append<T: Serialize + ?Sized + 'static>(value: &T) -> Self

Available on crate feature append only.

Creates a Mutations containing a single Append mutation with the given value.

The value is wrapped in a Box<dyn Serialize> via [SerializeRef], allowing unsized types like str and [T] to be used.

Trait Implementations§

Source§

impl<V: Clone> Clone for Mutations<V>

Source§

fn clone(&self) -> Mutations<V>

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<V: Debug> Debug for Mutations<V>

Source§

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

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

impl<V> Default for Mutations<V>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<V> From<MutationKind<V>> for Mutations<V>

Source§

fn from(kind: MutationKind<V>) -> Self

Converts to this type from the input type.
Source§

impl<V> From<Mutations<V>> for Option<Mutation<V>>

Source§

fn from(value: Mutations<V>) -> Self

Converts to this type from the input type.
Source§

impl<V: PartialEq> PartialEq for Mutations<V>

Source§

fn eq(&self, other: &Mutations<V>) -> 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<V: Eq> Eq for Mutations<V>

Source§

impl<V> StructuralPartialEq for Mutations<V>

Auto Trait Implementations§

§

impl<V> Freeze for Mutations<V>
where V: Freeze,

§

impl<V> RefUnwindSafe for Mutations<V>
where V: RefUnwindSafe,

§

impl<V> Send for Mutations<V>
where V: Send,

§

impl<V> Sync for Mutations<V>
where V: Sync,

§

impl<V> Unpin for Mutations<V>
where V: Unpin,

§

impl<V> UnsafeUnpin for Mutations<V>
where V: UnsafeUnpin,

§

impl<V> UnwindSafe for Mutations<V>
where V: 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.