pub trait GeneralHandler<T: ?Sized>: Default {
type Spec;
// Required methods
fn on_observe(value: &mut T) -> Self;
fn on_deref_mut(&mut self);
fn on_collect(&self, value: &T) -> bool;
}Expand description
A handler trait for implementing change detection strategies in GeneralObserver.
GeneralHandler defines the interface for pluggable change detection strategies used
exclusively with GeneralObserver. Each handler implementation encapsulates a specific approach
to detecting whether a value has changed.
§Lifecycle
on_observe- Called once when observation beginson_deref_mut- Called each time the value is accessed viaDerefMuton_collect- Called once to determine if a change occurred
§Example
A ShallowObserver implementation that treats any mutation through
DerefMut as a complete replacement:
#[derive(Default)]
struct ShallowHandler {
mutated: bool,
}
impl<T> GeneralHandler<T> for ShallowHandler {
type Spec = DefaultSpec;
fn on_observe(_value: &mut T) -> Self {
Self { mutated: false }
}
fn on_deref_mut(&mut self) {
self.mutated = true;
}
fn on_collect(&self, _value: &T) -> bool {
self.mutated
}
}
type ShallowObserver<'i, T> = GeneralObserver<'i, T, ShallowHandler>;Required Associated Types§
Sourcetype Spec
type Spec
Associated specification type for GeneralObserver.
Required Methods§
Sourcefn on_observe(value: &mut T) -> Self
fn on_observe(value: &mut T) -> Self
Called when observation begins.
Sourcefn on_deref_mut(&mut self)
fn on_deref_mut(&mut self)
Called when the value is accessed through DerefMut.
Sourcefn on_collect(&self, value: &T) -> bool
fn on_collect(&self, value: &T) -> bool
Called when collecting changes, returns whether a change occurred.
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.