Skip to main content

DeriveMessage

Derive Macro DeriveMessage 

Source
#[derive(DeriveMessage)]
Expand description

Derive macro that generates a Pod-compatible wire struct from a domain struct.

Given a struct with bool, Option<numeric>, usize/isize, and #[repr(u8)] enum fields, generates {Name}Wire plus From conversions in both directions. Requires the derive feature.

#[derive(photon_ring::DeriveMessage)]
struct Order { price: f64, side: Side, filled: bool, tag: Option<u32> }
// Generates: OrderWire, From<Order> for OrderWire, From<OrderWire> for Order

Derive a Pod-compatible wire struct with From conversions.

Given a struct with fields that may include bool, Option<numeric>, usize/isize, and #[repr(u8)] enums, generates:

  1. {Name}Wire — a #[repr(C)] Clone + Copy struct with all fields converted to Pod-safe types, plus unsafe impl Pod.
  2. From<Name> for {Name}Wire — converts the domain struct to wire.
  3. From<{Name}Wire> for Name — converts the wire struct back.

§Field type mappings

Source typeWire typeTo wireFrom wire
f32, f64, u8..u128, i8..i128samepassthroughpassthrough
usizeu64as u64as usize
isizei64as i64as isize
boolu8if v { 1 } else { 0 }v != 0
Option<T> (T numeric)u64Some(x) => x as u64, None => 00 => None, v => Some(v as T)
[T; N] (T: Pod)samepassthroughpassthrough
Any other typeu8v as u8transmute(v)

§Enum safety

Enum fields are converted via transmute from u8. The enum must have #[repr(u8)] — the macro emits a compile-time size_of check to enforce this.

§Example

#[repr(u8)]
#[derive(Clone, Copy)]
enum Side { Buy = 0, Sell = 1 }

#[derive(photon_ring::Message)]
struct Order {
    price: f64,
    qty: u32,
    side: Side,
    filled: bool,
    tag: Option<u32>,
}
// Generates: OrderWire, From<Order> for OrderWire, From<OrderWire> for Order