Skip to main content

PayloadTransform

Trait PayloadTransform 

Source
pub trait PayloadTransform:
    Send
    + Sync
    + Debug {
    // Required methods
    fn transform_inbound(
        &self,
        data: Vec<u8>,
        aad: &[u8],
    ) -> Result<Vec<u8>, String>;
    fn transform_outbound(
        &self,
        data: Vec<u8>,
        aad: &[u8],
    ) -> Result<Vec<u8>, String>;

    // Provided method
    fn max_overhead(&self) -> usize { ... }
}
Expand description

Optional bidirectional payload transform, attached per-connection.

Applied by ttrpc at the wire boundary for all message types:

  • transform_inbound: after reading from transport, before dispatching
  • transform_outbound: after handler returns, before writing to transport

Application-defined. Typical uses:

  • Encryption/decryption (AES-256-GCM, ChaCha20-Poly1305)
  • No-op (plaintext, the default)

Compression should be performed at the application layer (before serialization into Request.payload), not as a PayloadTransform. This avoids payload-expansion edge cases and keeps the transform layer focused on security with predictable, fixed-size overhead.

Required Methods§

Source

fn transform_inbound( &self, data: Vec<u8>, aad: &[u8], ) -> Result<Vec<u8>, String>

Decrypt / decode a payload. aad is the authenticated-but-unencrypted header data (stream_id || type_ || flags); implementations using AEAD ciphers should pass it as Additional Authenticated Data so that header tampering is detected.

Source

fn transform_outbound( &self, data: Vec<u8>, aad: &[u8], ) -> Result<Vec<u8>, String>

Encrypt / encode a payload. aad has the same semantics as transform_inbound.

Provided Methods§

Source

fn max_overhead(&self) -> usize

Maximum number of bytes that transform_outbound may add to a payload.

Used by the framework to pre-check whether a raw payload will fit within MESSAGE_LENGTH_MAX after transform, without invoking the transform (which may advance internal state such as AEAD nonce counters).

The default (64) covers common AEAD schemes (AES-GCM: 28 bytes, ChaCha20-Poly1305: 28 bytes) with comfortable margin. Override for transforms with larger expansion.

Dyn Compatibility§

This trait is dyn compatible.

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

Implementors§