Crate solzempic_macros

Crate solzempic_macros 

Source
Expand description

Procedural macros for the Solzempic framework.

This crate provides compile-time code generation for Solana programs built with Solzempic. The macros reduce boilerplate while maintaining zero runtime overhead through compile-time expansion.

§Available Macros

MacroTypePurpose
[SolzempicDispatch]AttributeDispatch enum + framework types
instructionAttributeInstruction trait implementations
AccountDeriveAccount struct with discriminator

§Quick Start

use solzempic::SolzempicDispatch;

// 1. Define dispatch enum (generates framework types)
#[SolzempicDispatch("Your11111111111111111111111111111111111111")]
pub enum MyInstruction {
    Initialize = 0,
    Transfer = 1,
}

// 2. Define instruction struct
pub struct Transfer<'a> {
    from: AccountRefMut<'a, TokenAccount>,
    to: AccountRefMut<'a, TokenAccount>,
}

// 3. Implement with #[instruction] macro
#[instruction(TransferParams)]
impl<'a> Transfer<'a> {
    fn build(accounts: &'a [AccountInfo], params: &TransferParams) -> Result<Self, ProgramError> {
        // Parse accounts...
    }

    fn validate(&self, program_id: &Pubkey, params: &TransferParams) -> ProgramResult {
        // Validate state...
    }

    fn execute(&self, program_id: &Pubkey, params: &TransferParams) -> ProgramResult {
        // Execute logic...
    }
}

// 4. In entrypoint:
MyInstruction::process(program_id, accounts, instruction_data)?;

§Generated Code

§From SolzempicDispatch

  • ID - Program ID constant
  • Solzempic - Framework type implementing Framework trait
  • AccountRef<'a, T> - Type alias for read-only accounts
  • AccountRefMut<'a, T> - Type alias for writable accounts
  • ShardRefContext<'a, T> - Type alias for shard triplets
  • id() - Returns &'static Pubkey
  • TryFrom<u8> - Discriminator parsing
  • dispatch() - Handler dispatch (after enum construction)
  • process() - Direct dispatch (more efficient)

§From instruction

  • InstructionParams impl with associated Params type
  • Instruction<'a> impl with build, validate, execute methods

§From Account derive

  • #[repr(C)] for stable memory layout
  • Clone, Copy, Pod, Zeroable derives
  • Prepended discriminator: [u8; 8] field
  • Loadable impl for zero-copy loading

§Performance

All macros expand at compile time with zero runtime cost. The process() method is more efficient than dispatch() because it avoids constructing the enum variant before dispatching.

Attribute Macros§

SolzempicEntrypoint
account
Attribute macro for account structs.
instruction
Attribute macro for instruction impl blocks.

Derive Macros§

Account
Derive macro for account structs with automatic discriminator handling.