Skip to main content

Decode

Trait Decode 

Source
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. decode returns an owned E that lives past the envelope.
  • Borrowing codecs (rkyv): type Output<'a> = &'a <E as Archive>::Archived. decode returns 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 &E directly. 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§

Source

type Output<'a> where Self: 'a

What decode returns.

E for owning codecs, &'a E (or &'a Archived<E>) for zero-copy codecs. The where Self: 'a bound is the standard GAT shape and adds no real constraint for 'static codecs.

Source

type Error: Error + Send + Sync + 'static

The error type for deserialization failures.

Required Methods§

Source

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".

Implementors§

Source§

impl<E, F> Decode<E> for SerdeCodec<F>
where E: DeserializeOwned + Send + Sync + 'static, F: SerdeFormat,

Source§

type Output<'a> = E where Self: 'a

Source§

type Error = <F as SerdeFormat>::Error

Source§

impl<E> Decode<E> for BytemuckCodec
where E: AnyBitPattern + NoUninit + Send + Sync + 'static,

Source§

type Output<'a> = &'a E where Self: 'a

Source§

type Error = BytemuckError

Source§

impl<E> Decode<E> for RkyvCodec
where E: Archive + Send + Sync + 'static, E::Archived: for<'a> CheckBytes<HighValidator<'a, Error>>,

Source§

type Output<'a> = &'a <E as Archive>::Archived where Self: 'a

Source§

type Error = Error