Skip to main content

RefFromRow

Trait RefFromRow 

Source
pub trait RefFromRow<'a>: Sized {
    // Required method
    fn ref_from_row_binary(
        cols: &[FieldDescription<'_>],
        row: DataRow<'a>,
    ) -> Result<&'a Self>;
}
Expand description

Trait for zero-copy decoding of a row into a fixed-size struct.

Unlike FromRow, this trait returns a reference directly into the buffer without any copying or allocation. This requires:

  1. All fields are LengthPrefixed<T> where T: FixedWireSize
  2. No NULL values (columns must be NOT NULL)
  3. Struct has #[repr(C, packed)] layout

The derive macro generates zerocopy trait implementations automatically.

§Compile-fail tests

Missing #[repr(C, packed)]:

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

#[derive(RefFromRow)]
struct Invalid {
    value: LengthPrefixed<I32BE>,
}

Native integer types (must use big-endian wrappers):

use zero_postgres::conversion::ref_row::LengthPrefixed;
use zero_postgres_derive::RefFromRow;

#[derive(RefFromRow)]
#[repr(C, packed)]
struct Invalid {
    value: LengthPrefixed<i64>,
}

String fields are not allowed:

use zero_postgres_derive::RefFromRow;

#[derive(RefFromRow)]
#[repr(C, packed)]
struct Invalid {
    name: String,
}

Vec fields are not allowed:

use zero_postgres_derive::RefFromRow;

#[derive(RefFromRow)]
#[repr(C, packed)]
struct Invalid {
    data: Vec<u8>,
}

Required Methods§

Source

fn ref_from_row_binary( cols: &[FieldDescription<'_>], row: DataRow<'a>, ) -> Result<&'a Self>

Decode a row as a zero-copy reference from binary format.

§Errors

Returns an error if:

  • The row data size doesn’t match the struct size
  • Any column is NULL (indicated by length = -1)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§