pub trait RefFromRow<'buf>: Sized {
// Required method
fn ref_from_row(
cols: &[ColumnDefinition<'_>],
row: BinaryRowPayload<'buf>,
) -> Result<&'buf 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:
- All fields have fixed wire sizes (implement
FixedWireSize) - No NULL values (columns must be
NOT NULL) - Struct has
#[repr(C, packed)]layout
The derive macro generates zerocopy trait implementations automatically.
§Compile-fail tests
Missing #[repr(C, packed)]:
ⓘ
use zero_mysql::ref_row::I32LE;
use zero_mysql_derive::RefFromRow;
#[derive(RefFromRow)]
struct Invalid {
value: I32LE,
}Native integer types (must use little-endian wrappers):
ⓘ
use zero_mysql_derive::RefFromRow;
#[derive(RefFromRow)]
#[repr(C, packed)]
struct Invalid {
value: i64,
}String fields are not allowed:
ⓘ
use zero_mysql_derive::RefFromRow;
#[derive(RefFromRow)]
#[repr(C, packed)]
struct Invalid {
name: String,
}Vec fields are not allowed:
ⓘ
use zero_mysql_derive::RefFromRow;
#[derive(RefFromRow)]
#[repr(C, packed)]
struct Invalid {
data: Vec<u8>,
}Required Methods§
Sourcefn ref_from_row(
cols: &[ColumnDefinition<'_>],
row: BinaryRowPayload<'buf>,
) -> Result<&'buf Self>
fn ref_from_row( cols: &[ColumnDefinition<'_>], row: BinaryRowPayload<'buf>, ) -> Result<&'buf Self>
Decode a row as a zero-copy reference.
§Errors
Returns an error if:
- The row data size doesn’t match the struct size
- Any column is NULL (RefFromRow doesn’t support NULL)
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.