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
Required Methods§
Sourceunsafe fn collect_unchecked<A: Adapter>(
this: &mut Self,
) -> Result<Option<Mutation<A>>, A::Error>
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§
Sourcefn collect<A: Adapter>(this: &mut Self) -> Result<Option<Mutation<A>>, A::Error>
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
Errif 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 panickingDyn 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.