Skip to main content

Cursor

Struct Cursor 

Source
pub struct Cursor<'a> { /* private fields */ }
Expand description

Stateful, zero-copy cursor over an immutable byte slice.

All methods advance the cursor in-place and return sub-slices that borrow from the original input — no allocation, no memcpy.

Implementations§

Source§

impl<'a> Cursor<'a>

Source

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

Create a new cursor starting at position 0.

Source

pub const fn position(&self) -> usize

Current byte offset from the start.

Source

pub const fn remaining(&self) -> usize

Number of bytes remaining in the buffer.

Source

pub const fn is_empty(&self) -> bool

true if there are no more bytes to read.

Source

pub fn as_slice(&self) -> &'a [u8]

Return the unread portion as a slice without advancing.

Source

pub fn read_bytes(&mut self, n: usize) -> ParseResult<&'a [u8]>

Borrow exactly n bytes and advance the cursor.

This is the core primitive — O(1), zero-copy.

Source

pub fn read_u8(&mut self) -> ParseResult<u8>

Read a single byte.

Source

pub fn read_u16_le(&mut self) -> ParseResult<u16>

Read a little-endian u16.

Source

pub fn read_u32_le(&mut self) -> ParseResult<u32>

Read a little-endian u32.

Source

pub fn read_i32_le(&mut self) -> ParseResult<i32>

Read a little-endian i32.

Source

pub fn read_u64_le(&mut self) -> ParseResult<u64>

Read a little-endian u64.

Source

pub fn read_i64_le(&mut self) -> ParseResult<i64>

Read a little-endian i64.

Source

pub fn read_varint(&mut self) -> ParseResult<u64>

Parse a Bitcoin-style variable-length integer (1, 3, 5, or 9 bytes).

Returns the decoded value as u64.

Source

pub fn read_var_bytes(&mut self, max: usize) -> ParseResult<&'a [u8]>

Read a length-prefixed byte slice (varint + raw bytes).

Returns a zero-copy sub-slice of the original input.

Source

pub fn read_array<const N: usize>(&mut self) -> ParseResult<&'a [u8; N]>

Borrow a fixed-size array from the current position.

Panics at compile-time if N == 0.

Source

pub fn skip(&mut self, n: usize) -> ParseResult<()>

Skip n bytes without returning them.

Source

pub fn split(&mut self, n: usize) -> ParseResult<Cursor<'a>>

Create a sub-cursor for exactly n bytes and advance the outer cursor.

Useful for reading a known-size payload and ensuring it is fully consumed.

Trait Implementations§

Source§

impl<'a> Clone for Cursor<'a>

Source§

fn clone(&self) -> Cursor<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Cursor<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Cursor<'a>

§

impl<'a> RefUnwindSafe for Cursor<'a>

§

impl<'a> Send for Cursor<'a>

§

impl<'a> Sync for Cursor<'a>

§

impl<'a> Unpin for Cursor<'a>

§

impl<'a> UnsafeUnpin for Cursor<'a>

§

impl<'a> UnwindSafe for Cursor<'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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.