Skip to main content

Cursor

Struct Cursor 

Source
pub struct Cursor<B: Buf> { /* private fields */ }
Expand description

A stateful wrapper for sequential reading and writing in a buffer.

The Cursor keeps track of the current position within the underlying buffer B, ensuring that all read and write operations automatically advance the position and respect the buffer’s boundaries.

Implementations§

Source§

impl<B: Buf> Cursor<B>

Source

pub fn new(buffer: B) -> Self

Creates a new Cursor initialized at position 0.

Source

pub fn into_inner(self) -> B

Consumes the Cursor and returns the underlying buffer.

Source

pub fn position(&self) -> usize

Returns the current byte offset within the buffer.

Source§

impl<B: Buf> Cursor<B>

Source

pub fn remaining(&self) -> usize

Returns the number of bytes remaining until the end of the buffer.

Source

pub fn is_eof(&self) -> bool

Returns true if the cursor has reached the buffer length.

Source

pub fn advance(&mut self, amount: usize) -> BufResult<()>

Advances the cursor position by amount bytes.

§Errors

Returns BufError::UnexpectedEof if amount exceeds the remaining bytes.

Source

pub fn rewind(&mut self, amount: usize) -> BufResult<()>

Moves the cursor position backward by amount bytes.

§Errors

Returns BufError::OutOfBounds if amount exceeds the current position.

Source

pub fn seek_to(&mut self, position: usize) -> BufResult<()>

Sets the cursor position to an absolute position.

§Errors

Returns BufError::OutOfBounds if position exceeds the buffer’s length.

Source

pub fn seek_from_end(&mut self, offset_from_end: usize) -> BufResult<()>

Sets the cursor position relative to the end of the buffer.

§Errors

Returns BufError::OutOfBounds if offset_from_end exceeds the buffer’

Source

pub fn peek_u8(&self) -> BufResult<u8>

Peeks at a single byte at the current position without advancing the cursor.

Source

pub fn peek_u16_be(&self) -> BufResult<u16>

Peeks at a 16-bit big-endian integer at the current position without advancing.

Source

pub fn peek_u32_be(&self) -> BufResult<u32>

Peeks at a 32-bit big-endian integer at the current position without advancing.

Source

pub fn peek_array<const N: usize>(&self) -> BufResult<[u8; N]>

Peeks at a fixed-size byte array at the current position without advancing.

Source

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

Reads a single byte and advances the cursor by 1.

Source

pub fn read_u16_be(&mut self) -> BufResult<u16>

Reads a 16-bit big-endian integer and advances the cursor by 2.

Source

pub fn read_u32_be(&mut self) -> BufResult<u32>

Reads a 32-bit big-endian integer and advances the cursor by 4.

Source

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

Reads a fixed-size byte array and advances the cursor by N.

Source

pub fn read_into(&mut self, destination: &mut [u8]) -> BufResult<()>

Reads bytes into the provided destination and advances the cursor.

§Errors

Returns BufError::UnexpectedEof if the destination slice length exceeds the remaining bytes in the buffer.

Source§

impl<B: BufMut> Cursor<B>

Source

pub fn poke_u8(&mut self, val: u8) -> BufResult<()>

Writes a single byte at the current position without advancing the cursor.

Source

pub fn poke_u16_be(&mut self, val: u16) -> BufResult<()>

Writes a 16-bit big-endian integer at the current position without advancing.

Source

pub fn poke_u32_be(&mut self, val: u32) -> BufResult<()>

Writes a 32-bit big-endian integer at the current position without advancing.

Source

pub fn poke_array<const N: usize>(&mut self, val: &[u8; N]) -> BufResult<()>

Writes a fixed-size byte array at the current position without advancing.

Source

pub fn poke_slice(&mut self, src: &[u8]) -> BufResult<()>

Writes a byte slice at the current position without advancing the cursor.

Source

pub fn write_u8(&mut self, val: u8) -> BufResult<()>

Writes a single byte and advances the cursor by 1.

Source

pub fn write_u16_be(&mut self, val: u16) -> BufResult<()>

Writes a 16-bit big-endian integer and advances the cursor by 2.

Source

pub fn write_u32_be(&mut self, val: u32) -> BufResult<()>

Writes a 32-bit big-endian integer and advances the cursor by 4.

Source

pub fn write_array<const N: usize>(&mut self, val: &[u8; N]) -> BufResult<()>

Writes a fixed-size byte array and advances the cursor by N.

Source

pub fn write_slice(&mut self, src: &[u8]) -> BufResult<()>

Writes a byte slice and advances the cursor by the slice’s length.

§Errors

Returns BufError::OutOfBounds or BufError::UnexpectedEof depending on the underlying BufMut implementation if the slice exceeds buffer capacity.

Trait Implementations§

Source§

impl<B: Clone + Buf> Clone for Cursor<B>

Source§

fn clone(&self) -> Cursor<B>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<B: Copy + Buf> Copy for Cursor<B>

Source§

impl<B: Debug + Buf> Debug for Cursor<B>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<B> Freeze for Cursor<B>
where B: Freeze,

§

impl<B> RefUnwindSafe for Cursor<B>
where B: RefUnwindSafe,

§

impl<B> Send for Cursor<B>
where B: Send,

§

impl<B> Sync for Cursor<B>
where B: Sync,

§

impl<B> Unpin for Cursor<B>
where B: Unpin,

§

impl<B> UnsafeUnpin for Cursor<B>
where B: UnsafeUnpin,

§

impl<B> UnwindSafe for Cursor<B>
where B: UnwindSafe,

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