Skip to main content

SliceCursor

Struct SliceCursor 

Source
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>

Source

pub fn new(data: &'a [u8]) -> Self

Source

pub fn remaining(&self) -> usize

Bytes remaining from the current position.

Source

pub fn position(&self) -> usize

Current byte offset into the slice.

Source

pub fn read_u8(&mut self) -> Result<u8, ProgramError>

Source

pub fn read_u16(&mut self) -> Result<u16, ProgramError>

Source

pub fn read_u32(&mut self) -> Result<u32, ProgramError>

Source

pub fn read_u64(&mut self) -> Result<u64, ProgramError>

Source

pub fn read_i64(&mut self) -> Result<i64, ProgramError>

Source

pub fn read_bool(&mut self) -> Result<bool, ProgramError>

0false, anything else → true.

Source

pub fn read_address(&mut self) -> Result<Address, ProgramError>

Source

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.

Source

pub fn read_i8(&mut self) -> Result<i8, ProgramError>

Source

pub fn read_i16(&mut self) -> Result<i16, ProgramError>

Source

pub fn read_i32(&mut self) -> Result<i32, ProgramError>

Source

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.

Source

pub fn read_i128(&mut self) -> Result<i128, ProgramError>

Read an i128 (16 bytes, little-endian). For PnL tracking and signed price deltas.

Source

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.

Source

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()?;

Auto Trait Implementations§

§

impl<'a> Freeze for SliceCursor<'a>

§

impl<'a> RefUnwindSafe for SliceCursor<'a>

§

impl<'a> Send for SliceCursor<'a>

§

impl<'a> Sync for SliceCursor<'a>

§

impl<'a> Unpin for SliceCursor<'a>

§

impl<'a> UnsafeUnpin for SliceCursor<'a>

§

impl<'a> UnwindSafe for SliceCursor<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.