pub trait VersionedStateCodec<T>: Send + Sync {
// Required methods
fn schema_id(&self) -> &StateSchemaId;
fn current_version(&self) -> StateSchemaVersion;
fn encode(&self, value: &T) -> Result<Vec<u8>, StateCodecError>;
fn decode(&self, payload: &[u8]) -> Result<T, StateCodecError>;
// Provided method
fn upgrades(&self) -> &[StateSchemaUpgrade] { ... }
}Expand description
Serializer-neutral application codec for one durable-state schema.
Payloads are JSON objects represented as bytes so the public contract does not expose a particular serializer’s types. A codec may use Serde, manual JSON handling, or another implementation internally.
A codec declares its current version and the directed upgrades it can
apply. The framework accepts an equal or older recorded version, walks one
bounded deterministic chain of declared upgrades up to the current version,
and only then calls decode. A codec therefore parses
exactly one shape, and a recorded version newer than the current one is
rejected rather than truncated, defaulted, or reinterpreted.
Required Methods§
Sourcefn schema_id(&self) -> &StateSchemaId
fn schema_id(&self) -> &StateSchemaId
Returns the stable schema identifier.
Sourcefn current_version(&self) -> StateSchemaVersion
fn current_version(&self) -> StateSchemaVersion
Returns the version emitted by encode.
Sourcefn encode(&self, value: &T) -> Result<Vec<u8>, StateCodecError>
fn encode(&self, value: &T) -> Result<Vec<u8>, StateCodecError>
Encodes the current typed value as one JSON object.
§Errors
Returns a value-redacted codec classification.
Sourcefn decode(&self, payload: &[u8]) -> Result<T, StateCodecError>
fn decode(&self, payload: &[u8]) -> Result<T, StateCodecError>
Decodes one JSON-object payload already at
current_version.
§Errors
Returns StateCodecError::InvalidPayload when the payload does not
satisfy the current schema.
Provided Methods§
Sourcefn upgrades(&self) -> &[StateSchemaUpgrade]
fn upgrades(&self) -> &[StateSchemaUpgrade]
Declares the directed upgrades this codec can apply.
The default suits a codec whose schema has only ever had one version. A codec that has published an older version returns the edges that reach the current one; a recorded version with no path to the current version is rejected rather than guessed at.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".