ControlBlockState

Derive Macro ControlBlockState 

Source
#[derive(ControlBlockState)]
{
    // Attributes available to this derive:
    #[state]
}
Expand description

Derive macro for implementing EmbeddedState trait.

This macro generates implementations for types that can be stored in the ControlBlock’s 24-byte _reserved field for zero-copy state access.

§Requirements

The type must:

  • Be #[repr(C)] for stable memory layout
  • Be <= 24 bytes in size (checked at compile time)
  • Implement Clone, Copy, and Default
  • Contain only POD (Plain Old Data) types

§Attributes

  • #[state(version = N)] - Set state version for migrations (default: 1)

§Example

#[derive(ControlBlockState, Default, Clone, Copy)]
#[repr(C, align(8))]
#[state(version = 1)]
pub struct OrderBookState {
    pub best_bid: u64,    // 8 bytes
    pub best_ask: u64,    // 8 bytes
    pub order_count: u32, // 4 bytes
    pub _pad: u32,        // 4 bytes (padding for alignment)
}  // Total: 24 bytes - fits in ControlBlock._reserved

// Use with ControlBlockStateHelper:
let mut block = ControlBlock::new();
let state = OrderBookState { best_bid: 100, best_ask: 101, order_count: 42, _pad: 0 };
ControlBlockStateHelper::write_embedded(&mut block, &state)?;

§Size Validation

The macro generates a compile-time assertion that fails if the type exceeds 24 bytes:

#[derive(ControlBlockState, Default, Clone, Copy)]
#[repr(C)]
struct TooLarge {
    data: [u8; 32],  // 32 bytes - COMPILE ERROR!
}