field

Macro field 

Source
macro_rules! field {
    ($struct_expr:expr, $field:ident) => { ... };
    ($struct_expr:expr, $($field:ident).+) => { ... };
}
Expand description

Creates a field reference for direct struct field access (LEGACY API)

This macro captures the field name at compile time and creates a zero-cost field reference that can be used with MetricsContext methods.

§Examples

struct TradingMetrics {
    total_volume: u64,
    trade_count: u64,
}

let entity = TradingMetrics { total_volume: 0, trade_count: 0 };

// Create field references
let volume_field = field!(entity, total_volume);
let count_field = field!(entity, trade_count);

// Use with MetricsContext
ctx.get_ref(&volume_field)           // Option<u64>
ctx.set_ref(&count_field, 100)       // Set trade_count to 100
ctx.increment_ref(&count_field, 1)   // Increment by 1

// Nested fields also work
let price_field = field!(entity, reserves.last_price);
ctx.set_ref(&price_field, 123.45);

§Advantages over field_accessor!

  • No need to define separate accessor structs
  • Field names are validated at compile time
  • Type inference works automatically from struct definition
  • Less boilerplate code