SerializeObserver

Trait SerializeObserver 

Source
pub trait SerializeObserver<'i>: Observer<'i> {
    // Required method
    unsafe fn collect_unchecked<A: Adapter>(
        this: &mut Self,
    ) -> Result<Option<Mutation<A>>, A::Error>;

    // Provided method
    fn collect<A: Adapter>(
        this: &mut Self,
    ) -> Result<Option<Mutation<A>>, A::Error> { ... }
}
Expand description

Trait for observers that can serialize their collected mutations.

This trait extends Observer with the ability to collect and serialize mutations using a specific Adapter.

Required Methods§

Source

unsafe fn collect_unchecked<A: Adapter>( this: &mut Self, ) -> Result<Option<Mutation<A>>, A::Error>

Collects all recorded mutations (unsafe version).

§Safety

This method assumes the observer contains a valid (non-null) pointer. Calling this on a default-constructed observer results in undefined behavior.

Most users should call collect instead, which includes a null pointer check.

§Implementation Notes

Implementations can safely use Deref and DerefMut to access the observed value, as this method is only called when the observer contains a valid pointer. The observer’s Deref and DerefMut implementations are guaranteed to be safe when collect_unchecked is called.

unsafe fn collect_unchecked<A: Adapter>(this: Self) -> Result<Option<Mutation<A>>, A::Error> {
    // Safe to dereference
    collect_mutation(&*this)
}

Provided Methods§

Source

fn collect<A: Adapter>(this: &mut Self) -> Result<Option<Mutation<A>>, A::Error>

Collects all recorded mutations using the specified adapter.

  • Returns Ok(None) if no mutations is recorded or the observer is null
  • Returns Err if serialization fails.
§Example
use morphix::JsonAdapter;
use morphix::observe::{ObserveExt, SerializeObserverExt, ShallowObserver};

// Normal usage
let mut value = String::from("Hello");
let mut ob = value.observe();
ob += " world";
let mutation = ob.collect::<JsonAdapter>().unwrap();
assert!(mutation.is_some());

// Safe handling of default-constructed observer
let mut empty: ShallowObserver<i32> = Default::default();
let result = empty.collect::<JsonAdapter>().unwrap();
assert_eq!(result, None);   // Returns None instead of panicking

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.

Implementors§

Source§

impl<'i, H, S, N> SerializeObserver<'i> for GeneralObserver<'i, H, S, N>
where N: Unsigned, S: AsDerefMut<N, Target: Serialize> + 'i + ?Sized, H: GeneralHandler<S::Target>,