Skip to main content

Reader

Struct Reader 

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

A binary reader for parsing Bitcoin data structures.

Reader provides methods for reading integers in little-endian format and Bitcoin-specific variable-length integers (varints).

§Example

use bsv_rs::primitives::encoding::Reader;

let data = vec![0x01, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00];
let mut reader = Reader::new(&data);

assert_eq!(reader.read_u8().unwrap(), 0x01);
assert_eq!(reader.read_u16_le().unwrap(), 0x0002);
assert_eq!(reader.read_u32_le().unwrap(), 0x00000003);
assert!(reader.is_empty());

Implementations§

Source§

impl<'a> Reader<'a>

Source

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

Creates a new reader over the given byte slice.

§Arguments
  • data - The data to read from
§Returns

A new reader positioned at the beginning

Source

pub fn remaining(&self) -> usize

Returns the number of bytes remaining to be read.

Source

pub fn is_empty(&self) -> bool

Returns true if there are no more bytes to read.

Source

pub fn position(&self) -> usize

Returns the current read position.

Source

pub fn consumed_since(&self, from_pos: usize) -> &'a [u8]

Returns a slice of the underlying data from from_pos to the current position. Useful for capturing raw bytes consumed by a series of read operations.

Source

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

Reads a single unsigned byte.

Source

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

Reads a 16-bit unsigned integer in little-endian format.

Source

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

Reads a 32-bit unsigned integer in little-endian format.

Source

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

Reads a 64-bit unsigned integer in little-endian format.

Source

pub fn read_i8(&mut self) -> Result<i8>

Reads a single signed byte.

Source

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

Reads a 16-bit signed integer in little-endian format.

Source

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

Reads a 32-bit signed integer in little-endian format.

Source

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

Reads a 64-bit signed integer in little-endian format.

Source

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

Reads a Bitcoin variable-length integer.

Bitcoin varints are encoded as follows:

  • 0x00-0xFC: 1 byte, value as-is
  • 0xFD: 3 bytes total, 0xFD prefix + 2 bytes little-endian uint16
  • 0xFE: 5 bytes total, 0xFE prefix + 4 bytes little-endian uint32
  • 0xFF: 9 bytes total, 0xFF prefix + 8 bytes little-endian uint64
§Returns

The decoded value as a u64

§Example
use bsv_rs::primitives::encoding::Reader;

// Single byte: 0xFC (252)
let mut reader = Reader::new(&[0xFC]);
assert_eq!(reader.read_var_int().unwrap(), 0xFC);

// 3-byte: 0xFD 0xFD 0x00 (253)
let mut reader = Reader::new(&[0xFD, 0xFD, 0x00]);
assert_eq!(reader.read_var_int().unwrap(), 0xFD);
Source

pub fn read_var_int_num(&mut self) -> Result<usize>

Reads a Bitcoin varint as a usize, for use as a length.

This is a convenience method that converts the u64 result to usize. It will return an error if the value exceeds usize::MAX on 32-bit platforms.

Source

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

Reads a fixed number of bytes.

§Arguments
  • len - The number of bytes to read
§Returns

A slice of the requested bytes

Source

pub fn read_var_bytes(&mut self) -> Result<&'a [u8]>

Reads a variable-length byte sequence (varint length prefix followed by bytes).

This is commonly used in Bitcoin for scripts and other length-prefixed data.

§Returns

A slice of the bytes

Source

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

Reads all remaining bytes.

§Returns

A slice containing all bytes from the current position to the end

Trait Implementations§

Source§

impl<'a> Clone for Reader<'a>

Source§

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

§

impl<'a> RefUnwindSafe for Reader<'a>

§

impl<'a> Send for Reader<'a>

§

impl<'a> Sync for Reader<'a>

§

impl<'a> Unpin for Reader<'a>

§

impl<'a> UnsafeUnpin for Reader<'a>

§

impl<'a> UnwindSafe for Reader<'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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V