Skip to main content

ReadBuf

Struct ReadBuf 

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

Cursor over an immutable byte slice with a tracked read position.

§Example

use wire_codec::ReadBuf;

let mut buf = ReadBuf::new(&[0xDE, 0xAD, 0xBE, 0xEF]);
assert_eq!(buf.read_u16_be().unwrap(), 0xDEAD);
assert_eq!(buf.read_u16_be().unwrap(), 0xBEEF);
assert_eq!(buf.remaining(), 0);

Implementations§

Source§

impl<'a> ReadBuf<'a>

Source

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

Wrap bytes in a new cursor positioned at the start of the slice.

Source

pub const fn position(&self) -> usize

Current read offset within the backing slice.

Source

pub const fn remaining(&self) -> usize

Number of bytes still available for reading.

Source

pub const fn is_empty(&self) -> bool

Whether the cursor has consumed every byte of the backing slice.

Source

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

Slice of bytes that have not yet been consumed.

Source

pub fn peek(&self, n: usize) -> Result<&'a [u8]>

Look at the next n bytes without advancing the cursor.

§Errors

Returns Error::UnexpectedEof when fewer than n bytes remain.

Source

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

Read the next n bytes and advance the cursor by n.

§Errors

Returns Error::UnexpectedEof when fewer than n bytes remain.

Source

pub fn advance(&mut self, n: usize) -> Result<()>

Advance the cursor by n bytes without returning the skipped data.

§Errors

Returns Error::UnexpectedEof when fewer than n bytes remain.

Source

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

Read a single byte.

§Errors

Returns Error::UnexpectedEof when the cursor is exhausted.

Source

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

Read a big-endian u16.

Source

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

Read a little-endian u16.

Source

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

Read a big-endian u32.

Source

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

Read a little-endian u32.

Source

pub fn read_u64_be(&mut self) -> Result<u64>

Read a big-endian u64.

Source

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

Read a little-endian u64.

Trait Implementations§

Source§

impl<'a> Clone for ReadBuf<'a>

Source§

fn clone(&self) -> ReadBuf<'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 ReadBuf<'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 ReadBuf<'a>

§

impl<'a> RefUnwindSafe for ReadBuf<'a>

§

impl<'a> Send for ReadBuf<'a>

§

impl<'a> Sync for ReadBuf<'a>

§

impl<'a> Unpin for ReadBuf<'a>

§

impl<'a> UnsafeUnpin for ReadBuf<'a>

§

impl<'a> UnwindSafe for ReadBuf<'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> 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.