Skip to main content

Module lens

Module lens 

Source
Expand description

Cross-program account lenses – read foreign fields by offset.

Hopper lenses read fields from foreign account data by byte offset and type without importing the foreign program’s Rust type. This reduces compile-time coupling when the caller already has a reviewed layout contract.

§Safety

Lenses bypass type-level layout guarantees. The caller must know the correct offset and type for the target field. Incorrect offsets read garbage data, never out-of-bounds memory: every accessor is bounds-checked. Reference-returning lenses additionally hold a shared data borrow (a crate::borrow::Ref guard) for their lifetime, so they cannot coexist with an exclusive borrow of the same account’s data; by-value lenses copy through raw pointers and take no borrow.

§Usage

ⓘ
use hopper_native::lens;

// Read a 32-byte address at offset 10 from a foreign program's account
// (skip 10-byte Hopper header: disc + version + layout_id).
let authority = lens::read_address(oracle_account, 10)?;

// Read a u64 price at offset 42.
let price = lens::read_le_u64(oracle_account, 42)?;

// Read a typed struct at an offset.
let data: &MyPodType = lens::read_field::<MyPodType>(account, 10)?;

Functions§

field_eq
Compare a field in account data against an expected value without copying.
read_address
Read a 32-byte address from account data.
read_bool
Read a boolean from account data (0 = false, nonzero = true).
read_bytes
Read a byte slice from account data.
read_field
Read a Projectable field from account data at the given byte offset.
read_field_pod
Read a Pod field from account data at the given byte offset.
read_le_u16
Read a little-endian u16 from account data.
read_le_u32
Read a little-endian u32 from account data.
read_le_u64
Read a little-endian u64 from account data.
read_u8
Read a single byte from account data.