Skip to main content

Deserialized

Trait Deserialized 

Source
pub trait Deserialized: Sized {
    type Output<'a>: Send + Sync;
    type Error: Display;

    // Required method
    fn from_payload(payload: &[u8]) -> Result<Self::Output<'_>, Self::Error>;
}
Expand description

A type that deserializes itself from the payload bytes: Deserialize means the framework’s codec does it, Deserialized means it is already done by the user’s own type.

The core hands from_payload the delivery’s bytes exactly as they arrived - no codec runs, no decode policy of a codec applies, and nothing is copied: the output borrows the broker’s buffer. A failed construction is settled by the subscriber’s on_failure(decode = ..) policy, the same rung a codec decode failure lands on. This is the lane a zero-copy wire format (flatbuffers, capnp, a hand-rolled frame) rides without pretending to be serde.

§Implementing by hand

#[derive(Deserialized)] (under the macros feature) covers a newtype or single-field struct over &'a [u8]. Any other shape is a pair of short impls: the construction, and the Input spelling that routes the type onto the self-deserializing lane. The batch spelling comes for free - &[Frame<'_>] bodies mount off the same two impls:

use ruststream::prelude::*;

struct Frame<'a>(&'a [u8]);

impl Deserialized for Frame<'_> {
    type Output<'a> = Frame<'a>;
    type Error = core::convert::Infallible;

    fn from_payload(payload: &[u8]) -> Result<Frame<'_>, Self::Error> {
        Ok(Frame(payload))
    }
}

impl Input for Frame<'_> {
    type Axis = SoloDeserialized<Frame<'static>>;
}
let frame = Frame::from_payload(b"bytes")?;
assert_eq!(frame.0, b"bytes");

Required Associated Types§

Source

type Output<'a>: Send + Sync

This type at the payload’s lifetime (Frame<'a> for a borrowing Frame, Self for an owning one).

Source

type Error: Display

The construction failure; Infallible for a plain view over the bytes.

Required Methods§

Source

fn from_payload(payload: &[u8]) -> Result<Self::Output<'_>, Self::Error>

Constructs the value from one delivery’s payload, borrowing it.

§Errors

A constructor that validates (a flatbuffers verifier, a length check) reports the bad payload here; the subscriber’s decode failure policy settles the delivery.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§