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 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 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 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_u16_be(&mut self) -> Result<u16>
pub fn read_u16_be(&mut self) -> Result<u16>
Read a big-endian u16.
Sourcepub fn read_u16_le(&mut self) -> Result<u16>
pub fn read_u16_le(&mut self) -> Result<u16>
Read a little-endian u16.
Sourcepub fn read_u32_be(&mut self) -> Result<u32>
pub fn read_u32_be(&mut self) -> Result<u32>
Read a big-endian u32.
Sourcepub fn read_u32_le(&mut self) -> Result<u32>
pub fn read_u32_le(&mut self) -> Result<u32>
Read a little-endian u32.
Sourcepub fn read_u64_be(&mut self) -> Result<u64>
pub fn read_u64_be(&mut self) -> Result<u64>
Read a big-endian u64.
Sourcepub fn read_u64_le(&mut self) -> Result<u64>
pub fn read_u64_le(&mut self) -> Result<u64>
Read a little-endian u64.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more