Observe

Trait Observe 

Source
pub trait Observe: Serialize {
    type Observer<'i>: Observer<'i, Self>
       where Self: 'i;

    // Provided methods
    fn observe<'i>(&'i mut self) -> Self::Observer<'i> { ... }
    fn serialize_append<S: Serializer>(
        &self,
        serializer: S,
        start_index: usize,
    ) -> Result<S::Ok, S::Error> { ... }
}
Expand description

A trait for types that can be observed for mutations.

Types implementing Observe can be wrapped in [Observers] that track mutations. The trait is typically derived using the #[derive(Observe)] macro.

§Example

use morphix::Observe;
use serde::Serialize;

#[derive(Serialize, Observe)]
struct MyStruct {
    field: String,
}

let mut data = MyStruct { field: "value".to_string() };
let mut data_observer = data.observe();
// Mutations through observer are tracked
data_observer.field.push_str(" modified");

Required Associated Types§

Source

type Observer<'i>: Observer<'i, Self> where Self: 'i

Associated observer type.

Provided Methods§

Source

fn observe<'i>(&'i mut self) -> Self::Observer<'i>

Creates an observer for this value.

§Returns

An observer that wraps this value and tracks mutations.

Source

fn serialize_append<S: Serializer>( &self, serializer: S, start_index: usize, ) -> Result<S::Ok, S::Error>

Serializes only the appended portion of the value.

This method is used for optimizing append operations by only serializing the new data rather than the entire value.

§Arguments
  • serializer - serializer to use
  • start_index - index from which to start serialization
§Errors
  • Returns serialization errors from the underlying serializer.
§Panics
  • Panics if called on types that don’t support append operations.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Observe for bool

Source§

type Observer<'i> = ShallowObserver<'i, bool> where Self: 'i

Source§

impl Observe for f32

Source§

type Observer<'i> = ShallowObserver<'i, f32> where Self: 'i

Source§

impl Observe for f64

Source§

type Observer<'i> = ShallowObserver<'i, f64> where Self: 'i

Source§

impl Observe for i8

Source§

type Observer<'i> = ShallowObserver<'i, i8> where Self: 'i

Source§

impl Observe for i16

Source§

type Observer<'i> = ShallowObserver<'i, i16> where Self: 'i

Source§

impl Observe for i32

Source§

type Observer<'i> = ShallowObserver<'i, i32> where Self: 'i

Source§

impl Observe for i64

Source§

type Observer<'i> = ShallowObserver<'i, i64> where Self: 'i

Source§

impl Observe for i128

Source§

type Observer<'i> = ShallowObserver<'i, i128> where Self: 'i

Source§

impl Observe for isize

Source§

type Observer<'i> = ShallowObserver<'i, isize> where Self: 'i

Source§

impl Observe for u8

Source§

type Observer<'i> = ShallowObserver<'i, u8> where Self: 'i

Source§

impl Observe for u16

Source§

type Observer<'i> = ShallowObserver<'i, u16> where Self: 'i

Source§

impl Observe for u32

Source§

type Observer<'i> = ShallowObserver<'i, u32> where Self: 'i

Source§

impl Observe for u64

Source§

type Observer<'i> = ShallowObserver<'i, u64> where Self: 'i

Source§

impl Observe for u128

Source§

type Observer<'i> = ShallowObserver<'i, u128> where Self: 'i

Source§

impl Observe for usize

Source§

type Observer<'i> = ShallowObserver<'i, usize> where Self: 'i

Source§

impl Observe for String

Source§

type Observer<'i> = StringObserver<'i> where Self: 'i

Source§

fn serialize_append<S: Serializer>( &self, serializer: S, start_index: usize, ) -> Result<S::Ok, S::Error>

Source§

impl<T: Observe> Observe for Vec<T>

Source§

type Observer<'i> = VecObserver<'i, T> where Self: 'i

Source§

fn serialize_append<S: Serializer>( &self, serializer: S, start_index: usize, ) -> Result<S::Ok, S::Error>

Implementors§