Skip to main content

RefFromRow

Derive Macro RefFromRow 

Source
#[derive(RefFromRow)]
Expand description

Derive macro for RefFromRow trait - zero-copy row decoding.

This macro generates a zero-copy implementation that returns a reference directly into the row buffer. It also derives zerocopy traits automatically.

§Requirements

  • Struct must have #[repr(C, packed)] attribute
  • All fields must be LengthPrefixed<T> where T implements FixedWireSize
  • All columns must be NOT NULL (no Option<T> support)
  • Only works with binary format (extended queries)

§PostgreSQL Wire Format

PostgreSQL’s binary protocol includes a 4-byte length prefix before each column value. Use LengthPrefixed<T> to account for this in the struct layout.

§Example

use zero_postgres::conversion::ref_row::{RefFromRow, LengthPrefixed, I64BE, I32BE};

#[derive(RefFromRow)]
#[repr(C, packed)]
struct UserStats {
    user_id: LengthPrefixed<I64BE>,     // 4 + 8 = 12 bytes
    login_count: LengthPrefixed<I32BE>, // 4 + 4 = 8 bytes
}
// Total wire size: 20 bytes