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§
Provided Methods§
Sourcefn observe<'i>(&'i mut self) -> Self::Observer<'i>
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.
Sourcefn serialize_append<S: Serializer>(
&self,
serializer: S,
start_index: usize,
) -> Result<S::Ok, S::Error>
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 usestart_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.