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>
impl<'a> ReadBuf<'a>
Sourcepub const fn new(bytes: &'a [u8]) -> Self
pub const fn new(bytes: &'a [u8]) -> Self
Wrap bytes in a new cursor positioned at the start of the slice.
Sourcepub const fn position(&self) -> usize
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.
Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Whether the cursor has consumed every byte of the backing slice.
Sourcepub fn remaining_slice(&self) -> &'a [u8] ⓘ
pub fn remaining_slice(&self) -> &'a [u8] ⓘ
Slice of bytes that have not yet been consumed.
Sourcepub fn peek(&self, n: usize) -> Result<&'a [u8]>
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.
Sourcepub fn peek_u8(&self) -> Result<u8>
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);Sourcepub fn read_bytes(&mut self, n: usize) -> Result<&'a [u8]>
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.
Sourcepub fn read_array<const N: usize>(&mut self) -> Result<[u8; N]>
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");Sourcepub fn read_array_ref<const N: usize>(&mut self) -> Result<&'a [u8; N]>
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]);Sourcepub fn split_to(&mut self, n: usize) -> Result<ReadBuf<'a>>
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]);Sourcepub fn advance(&mut self, n: usize) -> Result<()>
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.
Sourcepub fn read_i8(&mut self) -> Result<i8>
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.
Sourcepub fn read_u16_be(&mut self) -> Result<u16>
pub fn read_u16_be(&mut self) -> Result<u16>
Sourcepub fn read_u16_le(&mut self) -> Result<u16>
pub fn read_u16_le(&mut self) -> Result<u16>
Sourcepub fn read_u32_be(&mut self) -> Result<u32>
pub fn read_u32_be(&mut self) -> Result<u32>
Sourcepub fn read_u32_le(&mut self) -> Result<u32>
pub fn read_u32_le(&mut self) -> Result<u32>
Sourcepub fn read_u64_be(&mut self) -> Result<u64>
pub fn read_u64_be(&mut self) -> Result<u64>
Sourcepub fn read_u64_le(&mut self) -> Result<u64>
pub fn read_u64_le(&mut self) -> Result<u64>
Sourcepub fn read_u128_be(&mut self) -> Result<u128>
pub fn read_u128_be(&mut self) -> Result<u128>
Sourcepub fn read_u128_le(&mut self) -> Result<u128>
pub fn read_u128_le(&mut self) -> Result<u128>
Sourcepub fn read_i16_be(&mut self) -> Result<i16>
pub fn read_i16_be(&mut self) -> Result<i16>
Sourcepub fn read_i16_le(&mut self) -> Result<i16>
pub fn read_i16_le(&mut self) -> Result<i16>
Sourcepub fn read_i32_be(&mut self) -> Result<i32>
pub fn read_i32_be(&mut self) -> Result<i32>
Sourcepub fn read_i32_le(&mut self) -> Result<i32>
pub fn read_i32_le(&mut self) -> Result<i32>
Sourcepub fn read_i64_be(&mut self) -> Result<i64>
pub fn read_i64_be(&mut self) -> Result<i64>
Sourcepub fn read_i64_le(&mut self) -> Result<i64>
pub fn read_i64_le(&mut self) -> Result<i64>
Sourcepub fn read_i128_be(&mut self) -> Result<i128>
pub fn read_i128_be(&mut self) -> Result<i128>
Sourcepub fn read_i128_le(&mut self) -> Result<i128>
pub fn read_i128_le(&mut self) -> Result<i128>
Sourcepub fn read_f32_be(&mut self) -> Result<f32>
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.
Sourcepub fn read_f32_le(&mut self) -> Result<f32>
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.
Sourcepub fn read_f64_be(&mut self) -> Result<f64>
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.
Sourcepub fn read_f64_le(&mut self) -> Result<f64>
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.