Skip to main content

Cursor

Struct Cursor 

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

A no-std compatible Cursor for reading from byte slices.

Wraps a &[u8] and tracks a read position, implementing both sync::Read and sync::Seek (when the sync feature is enabled).

§Example

use hadris_io::{Cursor, Read, Seek, SeekFrom};

let data = [1u8, 2, 3, 4, 5];
let mut cursor = Cursor::new(&data);

let mut buf = [0u8; 2];
cursor.read_exact(&mut buf).unwrap();
assert_eq!(buf, [1, 2]);

cursor.seek(SeekFrom::Start(0)).unwrap();
cursor.read_exact(&mut buf).unwrap();
assert_eq!(buf, [1, 2]);

Implementations§

Source§

impl<'a> Cursor<'a>

Source

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

Creates a new cursor wrapping the given byte slice, starting at position 0.

use hadris_io::Cursor;

let data = [1, 2, 3];
let cursor = Cursor::new(&data);
assert_eq!(cursor.position(), 0);
assert_eq!(cursor.get_ref().len(), 3);
Source

pub fn position(&self) -> usize

Returns the current byte offset within the underlying data.

use hadris_io::Cursor;

let mut cursor = Cursor::new(&[0u8; 10]);
assert_eq!(cursor.position(), 0);
cursor.set_position(5);
assert_eq!(cursor.position(), 5);
Source

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

Sets the cursor position to the given byte offset.

use hadris_io::Cursor;

let mut cursor = Cursor::new(&[0u8; 10]);
cursor.set_position(7);
assert_eq!(cursor.position(), 7);
Source

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

Returns a reference to the underlying byte slice.

use hadris_io::Cursor;

let data = [1, 2, 3];
let cursor = Cursor::new(&data);
assert_eq!(cursor.get_ref(), &[1, 2, 3]);

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 (const: unstable) · 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
Source§

impl Read for Cursor<'_>

Available on crate feature sync only.
Source§

type Error = ErrorKind

Error returned by the underlying source.
Source§

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

Read some bytes, returning zero at end of input.
Source§

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

Fill buf, retrying interrupted operations.
Source§

impl Seek for Cursor<'_>

Available on crate feature sync only.
Source§

type Error = ErrorKind

Error returned by the underlying stream.
Source§

fn seek(&mut self, pos: SeekFrom) -> Result<u64>

Seek to a new byte position.
Source§

fn stream_position(&mut self) -> Result<u64, Self::Error>

Return the current byte position.
Source§

fn seek_relative(&mut self, offset: i64) -> Result<(), Self::Error>

Seek relative to the current byte position.

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> ReadExt for T
where T: Read + ?Sized,

Source§

fn read_struct<T: Zeroable + NoUninit + AnyBitPattern>(&mut self) -> Result<T>

Read an arbitrary-bit-pattern value.
Source§

fn parse<T: Parsable>(&mut self) -> Result<T>
where Self: Sized,

Parse a value with its custom parser.
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.