Skip to main content

Module option_byte

Module option_byte 

Source
Expand description

Zero-copy, tag-validated optional values for instruction args.

Rust’s Option<T> has niche-optimizing layout rules that make it unsafe to pointer-cast from raw instruction bytes. Option<u8> is two bytes with an undefined tag range; Option<&T> uses null for None. Neither is a layout the caller controls.

OptionByte<T> is the Hopper replacement for args. Layout:

#[repr(C)]
{ tag: u8, value: T }

tag == 0 is None, tag == 1 is Some. Any other tag byte is a protocol error and OptionByte::get surfaces it as ProgramError::InvalidInstructionData. This mirrors Quasar’s OptionZc<T>::validate_zc contract with one fewer type parameter and no MaybeUninit escape hatch.

§Usage

#[hopper::args]
#[repr(C)]
pub struct SwapArgs {
    pub amount: u64,
    pub referrer: OptionByte<[u8; 32]>,
    pub slippage_bps: u16,
}

fn handler(ctx: Context<Swap>, args: &SwapArgs) -> ProgramResult {
    if let Some(referrer) = args.referrer.get()? {
        // referrer is &[u8; 32]
    }
    Ok(())
}

Structs§

OptionByte
Zero-copy tagged optional. See module docs for the layout and usage contract.