pub trait Decode<E: ?Sized>:
Send
+ Sync
+ 'static {
type Output<'a>
where Self: 'a;
type Error: Error + Send + Sync + 'static;
// Required method
fn decode<'a>(
&'a self,
env: &'a PersistedEnvelope,
) -> Result<Self::Output<'a>, Self::Error>;
}Expand description
Decode a persisted envelope to a typed value (or borrow into it).
One trait covers both owning and borrowing codecs via the
Output<'a> GAT:
- Owning codecs (serde JSON/bincode/postcard):
type Output<'a> = E.decodereturns an ownedEthat lives past the envelope. - Borrowing codecs (rkyv):
type Output<'a> = &'a <E as Archive>::Archived.decodereturns a reference into the envelope’s payload bytes — no allocation, no copy. - Plain-old-data codecs (bytemuck):
type Output<'a> = &'a E. Reinterprets the payload as&Edirectly. Relies on the wire-format 16-byte payload-alignment invariant for safety.
The two-trait split (previously Decode + BorrowingDecode) was a
workaround for the borrowed-cursor lifetime cliff: when the envelope
was borrowed from a cursor row that died on .next(), the same
operation needed two trait shapes. The owned-Bytes envelope removes
the cliff — 'a now ties to the envelope itself, which is cheap-to-
clone and lifetime-independent.
E: ?Sized allows unsized event types: Decode<[u8]>, Decode<str>,
Decode<<E as Archive>::Archived>.
Independent from Encode: a codec may implement only Decode
(read-only replica), only Encode (write-only shipper), or both.
Required Associated Types§
Required Methods§
Sourcefn decode<'a>(
&'a self,
env: &'a PersistedEnvelope,
) -> Result<Self::Output<'a>, Self::Error>
fn decode<'a>( &'a self, env: &'a PersistedEnvelope, ) -> Result<Self::Output<'a>, Self::Error>
Decode the envelope’s payload to Output<'a>.
The envelope is the input: its event_type()
is the variant discriminant, payload()
is the serialized bytes. Codecs reach into the envelope for whatever
they need — no pre-extracted arguments.
§Errors
Returns Self::Error if the payload is invalid (e.g. failed archive
validation for rkyv) or does not match the type discriminant.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".