Skip to main content

BlockReader

Struct BlockReader 

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

Cursor-based TLV field reader for block bodies.

BlockReader wraps a byte slice and provides an iterator-like interface for consuming TLV fields one at a time. It delegates to the decode functions in bcp_types::fields but adds a stateful cursor so the caller doesn’t have to manually track offsets.

This is an internal implementation detail of the decoder — it is not part of the public API.

§Usage pattern

  let mut reader = BlockReader::new(body);
  while let Some(field) = reader.next_field()? {
      match field.field_id {
          1 => { /* handle field 1 */ }
          2 => { /* handle field 2 */ }
          _ => { /* skip unknown */ }
      }
  }

Implementations§

Source§

impl<'a> BlockReader<'a>

Source

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

Create a new reader over the given body bytes.

The reader starts at position 0 and advances through the buffer as fields are consumed via next_field.

Source

pub fn next_field(&mut self) -> Result<Option<RawField<'a>>, DecodeError>

Read the next TLV field from the body.

Returns Ok(Some(field)) if a field was successfully read, or Ok(None) when the buffer is exhausted. Returns Err if the field header or payload is malformed.

For Varint fields, data points to the varint bytes in the original buffer (the caller should use decode_varint_value to extract the value). For Bytes and Nested fields, data is the raw payload after the length prefix.

§Errors

Returns DecodeError::Type or DecodeError::Wire if the field header is malformed or the payload is truncated.

Source

pub fn position(&self) -> usize

Return the number of bytes consumed so far.

Source

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

Return the remaining unread bytes.

Auto Trait Implementations§

§

impl<'a> Freeze for BlockReader<'a>

§

impl<'a> RefUnwindSafe for BlockReader<'a>

§

impl<'a> Send for BlockReader<'a>

§

impl<'a> Sync for BlockReader<'a>

§

impl<'a> Unpin for BlockReader<'a>

§

impl<'a> UnsafeUnpin for BlockReader<'a>

§

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