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.

Because every read is atomic, after an error this is the offset of the value that failed to decode, which makes it useful in diagnostics.

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 peek_u8(&self) -> Result<u8>

Look at the next byte without advancing the cursor.

§Errors

Returns Error::UnexpectedEof when the cursor is exhausted.

§Example
use wire_codec::ReadBuf;

let buf = ReadBuf::new(&[0x7F]);
assert_eq!(buf.peek_u8().unwrap(), 0x7F);
assert_eq!(buf.position(), 0);
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 read_array<const N: usize>(&mut self) -> Result<[u8; N]>

Read the next N bytes as a fixed-size array (copied).

§Errors

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

§Example
use wire_codec::ReadBuf;

let mut buf = ReadBuf::new(b"GIF89a...");
let magic: [u8; 6] = buf.read_array().unwrap();
assert_eq!(&magic, b"GIF89a");
Source

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

Borrow the next N bytes as a fixed-size array reference (zero-copy).

§Errors

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

§Example
use wire_codec::ReadBuf;

let data = [1, 2, 3, 4, 5];
let mut buf = ReadBuf::new(&data);
let head: &[u8; 4] = buf.read_array_ref().unwrap();
assert_eq!(head, &[1, 2, 3, 4]);
Source

pub fn split_to(&mut self, n: usize) -> Result<ReadBuf<'a>>

Split off the next n bytes as an independent cursor and advance past them.

Useful for a length-delimited sub-record: decode it from the returned cursor, then check ReadBuf::is_empty to make sure it was consumed exactly.

§Errors

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

§Example
use wire_codec::ReadBuf;

let mut buf = ReadBuf::new(&[0x02, 0xAA, 0xBB, 0xCC]);
let len = usize::from(buf.read_u8().unwrap());
let mut record = buf.split_to(len).unwrap();
assert_eq!(record.read_u16_be().unwrap(), 0xAABB);
assert!(record.is_empty());
assert_eq!(buf.remaining_slice(), &[0xCC]);
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_i8(&mut self) -> Result<i8>

Read a single byte as an i8 (two’s complement).

§Errors

Returns Error::UnexpectedEof when the cursor is exhausted.

Source

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

Read a big-endian u16.

§Errors

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

Source

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

Read a little-endian u16.

§Errors

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

Source

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

Read a big-endian u32.

§Errors

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

Source

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

Read a little-endian u32.

§Errors

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

Source

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

Read a big-endian u64.

§Errors

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

Source

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

Read a little-endian u64.

§Errors

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

Source

pub fn read_u128_be(&mut self) -> Result<u128>

Read a big-endian u128.

§Errors

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

Source

pub fn read_u128_le(&mut self) -> Result<u128>

Read a little-endian u128.

§Errors

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

Source

pub fn read_i16_be(&mut self) -> Result<i16>

Read a big-endian i16.

§Errors

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

Source

pub fn read_i16_le(&mut self) -> Result<i16>

Read a little-endian i16.

§Errors

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

Source

pub fn read_i32_be(&mut self) -> Result<i32>

Read a big-endian i32.

§Errors

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

Source

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

Read a little-endian i32.

§Errors

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

Source

pub fn read_i64_be(&mut self) -> Result<i64>

Read a big-endian i64.

§Errors

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

Source

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

Read a little-endian i64.

§Errors

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

Source

pub fn read_i128_be(&mut self) -> Result<i128>

Read a big-endian i128.

§Errors

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

Source

pub fn read_i128_le(&mut self) -> Result<i128>

Read a little-endian i128.

§Errors

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

Source

pub fn read_f32_be(&mut self) -> Result<f32>

Read a big-endian IEEE 754 f32.

§Errors

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

Source

pub fn read_f32_le(&mut self) -> Result<f32>

Read a little-endian IEEE 754 f32.

§Errors

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

Source

pub fn read_f64_be(&mut self) -> Result<f64>

Read a big-endian IEEE 754 f64.

§Errors

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

Source

pub fn read_f64_le(&mut self) -> Result<f64>

Read a little-endian IEEE 754 f64.

§Errors

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

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Self

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> Forget for ReadBuf<'a>

§

impl<'a> Freeze for ReadBuf<'a>

§

impl<'a> Move 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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.