Cursor

Struct Cursor 

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

A cursor which wraps an in-memory buffer and provides positioned reading

Cursor is a simple wrapper that allows reading from various in-memory data types (like &[u8]) with a tracked position. This is useful for parsing protocols where you need to read sequentially through a buffer.

Unlike std::io::Cursor, this implementation is designed for no_std environments and focuses on reading operations only.

§Examples

use mqtt_protocol_core::mqtt::common::Cursor;

let data = &b"hello world"[..];
let mut cursor = Cursor::new(data);

// Read individual bytes
assert_eq!(cursor.read_u8(), Some(b'h'));
assert_eq!(cursor.position(), 1);

// Read multiple bytes at once
let chunk = cursor.read_bytes(5).unwrap();
assert_eq!(chunk, b"ello ");
assert_eq!(cursor.position(), 6);

Implementations§

Source§

impl<T> Cursor<T>

Source

pub fn new(inner: T) -> Self

Creates a new cursor with the provided data

The cursor starts at position 0.

§Examples
use mqtt_protocol_core::mqtt::common::Cursor;

let cursor = Cursor::new(&b"hello"[..]);
assert_eq!(cursor.position(), 0);
Source

pub fn position(&self) -> u64

Returns the current position of the cursor

§Returns

The current position as a u64 value

§Examples
use mqtt_protocol_core::mqtt::common::Cursor;

let mut cursor = Cursor::new(&b"hello"[..]);
assert_eq!(cursor.position(), 0);

cursor.set_position(3);
assert_eq!(cursor.position(), 3);
Source

pub fn set_position(&mut self, pos: u64)

Sets the position of the cursor

§Parameters
  • pos - New position for the cursor
§Examples
use mqtt_protocol_core::mqtt::common::Cursor;

let mut cursor = Cursor::new(&b"hello"[..]);
cursor.set_position(3);
assert_eq!(cursor.position(), 3);
Source

pub fn get_ref(&self) -> &T

Gets a reference to the underlying value

§Returns

A reference to the underlying data of type &T

§Examples
use mqtt_protocol_core::mqtt::common::Cursor;

let data = &b"hello"[..];
let cursor = Cursor::new(data);
assert_eq!(cursor.get_ref(), &data);
Source§

impl Cursor<&[u8]>

Source

pub fn remaining_slice(&self) -> &[u8]

Returns a slice of the remaining unread data

This returns all data from the current position to the end of the buffer. If the position is beyond the buffer length, returns an empty slice.

§Returns
  • &[u8] - Slice containing all unread data from current position to end
§Examples
use mqtt_protocol_core::mqtt::common::Cursor;

let mut cursor = Cursor::new(&b"hello"[..]);
cursor.set_position(2);
assert_eq!(cursor.remaining_slice(), b"llo");
Source

pub fn read_bytes(&mut self, count: usize) -> Option<&[u8]>

Reads exactly count bytes from the cursor

Advances the cursor position by count bytes and returns a slice to the read data. Returns None if there are not enough bytes remaining to satisfy the request.

§Parameters
  • count - Number of bytes to read
§Returns
  • Some(&[u8]) - Slice containing exactly count bytes
  • None - Not enough data available to read count bytes
§Examples
use mqtt_protocol_core::mqtt::common::Cursor;

let mut cursor = Cursor::new(&b"hello world"[..]);
assert_eq!(cursor.read_bytes(5), Some(&b"hello"[..]));
assert_eq!(cursor.position(), 5);
assert_eq!(cursor.read_bytes(20), None); // Not enough data
Source

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

Reads a single byte from the cursor

Advances the cursor position by 1 byte and returns the byte value. Returns None if there are no more bytes to read.

§Returns
  • Some(u8) - The next byte from the cursor
  • None - No more bytes available to read
§Examples
use mqtt_protocol_core::mqtt::common::Cursor;

let mut cursor = Cursor::new(&b"hi"[..]);
assert_eq!(cursor.read_u8(), Some(b'h'));
assert_eq!(cursor.read_u8(), Some(b'i'));
assert_eq!(cursor.read_u8(), None); // End of data
Source§

impl<T: AsRef<[u8]>> Cursor<T>

Source

pub fn read(&mut self, buf: &mut [u8]) -> Result<usize, CursorError>

Pulls some bytes from this cursor into the specified buffer

This method is compatible with std::io::Read::read(). It reads at most buf.len() bytes from the cursor into the provided buffer, advancing the cursor position accordingly.

§Parameters
  • buf - Buffer to read data into
§Returns
  • Ok(n) - Number of bytes read (0 to buf.len())

This method currently never returns an error, but returns a Result for compatibility with std::io::Read::read().

§Examples
use mqtt_protocol_core::mqtt::common::Cursor;

let mut cursor = Cursor::new(&b"hello world"[..]);
let mut buf = [0u8; 5];
let n = cursor.read(&mut buf).unwrap();
assert_eq!(n, 5);
assert_eq!(&buf, b"hello");
assert_eq!(cursor.position(), 5);
Source

pub fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), CursorError>

Reads the exact number of bytes required to fill buf

This method is compatible with std::io::Read::read_exact(). It reads exactly buf.len() bytes from the cursor into the buffer, or returns an error if not enough data is available.

§Parameters
  • buf - Buffer to read data into (must be completely filled)
§Returns
  • Ok(()) - Successfully read exactly buf.len() bytes
  • Err(CursorError::UnexpectedEof) - Not enough data available
§Examples
use mqtt_protocol_core::mqtt::common::{Cursor, CursorError};

let mut cursor = Cursor::new(&b"hello"[..]);
let mut buf = [0u8; 3];
cursor.read_exact(&mut buf).unwrap();
assert_eq!(&buf, b"hel");

// Trying to read more than available
let mut buf2 = [0u8; 10];
assert_eq!(cursor.read_exact(&mut buf2), Err(CursorError::UnexpectedEof));

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for Cursor<T>
where T: 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> 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.