pub struct VersionedState<S> {
pub version: u32,
pub data: S,
}Expand description
Version-tagged wrapper for serialized widget state.
When persisting state, the registry wraps the raw state in this envelope so it can detect schema version mismatches on restore.
§Serialization
With the state-persistence feature enabled, VersionedState derives
Serialize and Deserialize. Without the feature, it is a plain struct
usable for in-memory versioning.
Fields§
§version: u32Schema version (from Stateful::state_version()).
data: SThe actual state payload.
Implementations§
Source§impl<S> VersionedState<S>
impl<S> VersionedState<S>
Sourcepub fn pack<W: Stateful<State = S>>(widget: &W) -> Self
pub fn pack<W: Stateful<State = S>>(widget: &W) -> Self
Pack a widget’s state into a versioned envelope.
Sourcepub fn unpack<W: Stateful<State = S>>(self) -> Option<S>
pub fn unpack<W: Stateful<State = S>>(self) -> Option<S>
Attempt to unpack, returning None if the version does not match
the widget’s current state_version().
Sourcepub fn unpack_or_default<W: Stateful<State = S>>(self) -> Swhere
S: Default,
pub fn unpack_or_default<W: Stateful<State = S>>(self) -> Swhere
S: Default,
Unpack with fallback: returns the stored data if versions match,
otherwise returns S::default().
Source§impl<S> VersionedState<S>
impl<S> VersionedState<S>
Sourcepub fn unpack_with_migration<W>(
self,
chain: &MigrationChain<S>,
) -> RestoreResult<S>
pub fn unpack_with_migration<W>( self, chain: &MigrationChain<S>, ) -> RestoreResult<S>
Attempt to unpack with migration support.
If the stored version doesn’t match the current version, attempts to migrate through the provided chain. Falls back to default on failure.
§Type Parameters
W: The widget type that implementsStateful<State = S>.
§Example
let chain = MigrationChain::new();
// ... register migrations ...
let versioned = load_state_from_disk();
let result = versioned.unpack_with_migration::<MyWidget>(&chain);
let state = result.into_state();Trait Implementations§
Source§impl<S: Clone> Clone for VersionedState<S>
impl<S: Clone> Clone for VersionedState<S>
Source§fn clone(&self) -> VersionedState<S>
fn clone(&self) -> VersionedState<S>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more