pub struct SliceCursor<'a> { /* private fields */ }Expand description
Zero-copy read cursor over a byte slice.
Tracks the current position and reads typed fields sequentially.
Every read is bounds-checked: you get AccountDataTooSmall instead
of a panic if you read past the end.
The typical pattern is to skip the discriminator byte and then read your fields in the order they appear in the account layout:
let data = account.try_borrow()?;
let mut cur = SliceCursor::new(&data[1..]); // skip discriminator
let balance = cur.read_u64()?;
let recipient = cur.read_address()?;
let flags = cur.read_u8()?;No alloc. No borsh schema. If the field order in your cursor doesn’t match your on-chain layout, you’ll get wrong values - that’s the only footgun. Keep a layout comment next to your account struct to avoid it.
Implementations§
Source§impl<'a> SliceCursor<'a>
impl<'a> SliceCursor<'a>
pub fn new(data: &'a [u8]) -> Self
pub fn read_u8(&mut self) -> Result<u8, ProgramError>
pub fn read_u16(&mut self) -> Result<u16, ProgramError>
pub fn read_u32(&mut self) -> Result<u32, ProgramError>
pub fn read_u64(&mut self) -> Result<u64, ProgramError>
pub fn read_i64(&mut self) -> Result<i64, ProgramError>
Sourcepub fn read_bool(&mut self) -> Result<bool, ProgramError>
pub fn read_bool(&mut self) -> Result<bool, ProgramError>
0 → false, anything else → true.
pub fn read_address(&mut self) -> Result<Address, ProgramError>
Sourcepub fn skip(&mut self, n: usize) -> Result<(), ProgramError>
pub fn skip(&mut self, n: usize) -> Result<(), ProgramError>
Skip n bytes without reading them. Useful for padding or fields
you don’t care about in the current instruction.
pub fn read_i8(&mut self) -> Result<i8, ProgramError>
pub fn read_i16(&mut self) -> Result<i16, ProgramError>
pub fn read_i32(&mut self) -> Result<i32, ProgramError>
Sourcepub fn read_u128(&mut self) -> Result<u128, ProgramError>
pub fn read_u128(&mut self) -> Result<u128, ProgramError>
Read a u128 (16 bytes, little-endian). Essential for DeFi price accumulators, LP math, and accumulated fee fields.
Sourcepub fn read_i128(&mut self) -> Result<i128, ProgramError>
pub fn read_i128(&mut self) -> Result<i128, ProgramError>
Read an i128 (16 bytes, little-endian). For PnL tracking and signed price deltas.
Sourcepub fn data_from_position(&self) -> &'a [u8]
pub fn data_from_position(&self) -> &'a [u8]
Return the remaining unread portion of the slice from the current position.
This is useful for handing off the rest of instruction data to a sub-parser after reading a tag/discriminator byte.
Sourcepub fn from_instruction(
data: &'a [u8],
min_len: usize,
) -> Result<Self, ProgramError>
pub fn from_instruction( data: &'a [u8], min_len: usize, ) -> Result<Self, ProgramError>
Create a cursor for instruction data with minimum length validation.
Returns an error if data.len() < min_len. Reinforces the pattern
of validating instruction data length before parsing.
let mut cur = SliceCursor::from_instruction(instruction_data, 9)?;
let tag = cur.read_u8()?;
let amount = cur.read_u64()?;