pub trait StatefulService: MockService {
type StateView: Serialize + DeserializeOwned + Send + Sync;
// Required methods
async fn snapshot(&self, account_id: &str, region: &str) -> Self::StateView;
async fn restore(
&self,
account_id: &str,
region: &str,
view: Self::StateView,
) -> Result<(), StateViewError>;
async fn merge(
&self,
account_id: &str,
region: &str,
view: Self::StateView,
) -> Result<(), StateViewError>;
fn notifier(&self) -> &StateChangeNotifier<Self::StateView>;
// Provided method
async fn notify_state_changed(&self, account_id: &str, region: &str) { ... }
}Expand description
A service that exposes typed, serde-compatible views of its internal state.
Each service implements this trait with a StateView type that is a
serde-friendly representation of its internal state. The view types
are separate from the internal types to decouple serialization from
implementation.
§Example
// Take a snapshot of S3 state
let view: S3StateView = s3_service.snapshot("123456789012", "us-east-1");
// Serialize to JSON
let json = serde_json::to_string_pretty(&view).unwrap();
// Restore from a view
let view: S3StateView = serde_json::from_str(&json).unwrap();
s3_service.restore("123456789012", "us-east-1", view).unwrap();Required Associated Types§
Required Methods§
Sourceasync fn snapshot(&self, account_id: &str, region: &str) -> Self::StateView
async fn snapshot(&self, account_id: &str, region: &str) -> Self::StateView
Take a snapshot of the state for the given account/region as a typed view.
Sourceasync fn restore(
&self,
account_id: &str,
region: &str,
view: Self::StateView,
) -> Result<(), StateViewError>
async fn restore( &self, account_id: &str, region: &str, view: Self::StateView, ) -> Result<(), StateViewError>
Restore state for the given account/region from a typed view. Replaces the existing state entirely.
Sourceasync fn merge(
&self,
account_id: &str,
region: &str,
view: Self::StateView,
) -> Result<(), StateViewError>
async fn merge( &self, account_id: &str, region: &str, view: Self::StateView, ) -> Result<(), StateViewError>
Merge a partial view into existing state (additive, does not remove existing resources).
Sourcefn notifier(&self) -> &StateChangeNotifier<Self::StateView>
fn notifier(&self) -> &StateChangeNotifier<Self::StateView>
Returns the state-change notifier embedded in this service.
Implement by returning a reference to a StateChangeNotifier field
on the service struct.
Provided Methods§
Sourceasync fn notify_state_changed(&self, account_id: &str, region: &str)
async fn notify_state_changed(&self, account_id: &str, region: &str)
Take a snapshot of the given account/region and forward it to all registered listeners. Call this after any successful state mutation.
The write lock on the state must be released before calling this method to avoid a deadlock (snapshot acquires a read lock).
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.