Skip to main content

Writer

Struct Writer 

Source
pub struct Writer { /* private fields */ }
Expand description

A binary writer for serializing Bitcoin data structures.

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

§Example

use bsv_rs::primitives::encoding::Writer;

let mut writer = Writer::new();
writer.write_u8(0x01);
writer.write_u16_le(0x0002);
writer.write_u32_le(0x00000003);

let data = writer.into_bytes();
assert_eq!(data, vec![0x01, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00]);

Implementations§

Source§

impl Writer

Source

pub fn new() -> Self

Creates a new empty writer.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new writer with pre-allocated capacity.

§Arguments
  • capacity - The number of bytes to pre-allocate
Source

pub fn len(&self) -> usize

Returns the current length of the written data.

Source

pub fn is_empty(&self) -> bool

Returns true if no data has been written.

Source

pub fn as_bytes(&self) -> &[u8]

Returns a reference to the written bytes.

Source

pub fn into_bytes(self) -> Vec<u8>

Consumes the writer and returns the written bytes.

Source

pub fn write_u8(&mut self, val: u8) -> &mut Self

Writes a single unsigned byte.

Source

pub fn write_u16_le(&mut self, val: u16) -> &mut Self

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

Source

pub fn write_u32_le(&mut self, val: u32) -> &mut Self

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

Source

pub fn write_u64_le(&mut self, val: u64) -> &mut Self

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

Source

pub fn write_i8(&mut self, val: i8) -> &mut Self

Writes a single signed byte.

Source

pub fn write_i16_le(&mut self, val: i16) -> &mut Self

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

Source

pub fn write_i32_le(&mut self, val: i32) -> &mut Self

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

Source

pub fn write_i64_le(&mut self, val: i64) -> &mut Self

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

Source

pub fn write_var_int(&mut self, val: u64) -> &mut Self

Writes a Bitcoin variable-length integer.

Bitcoin varints are encoded as follows:

  • 0x00-0xFC: 1 byte, value as-is
  • 0xFD-0xFFFF: 3 bytes, 0xFD prefix + 2 bytes little-endian uint16
  • 0x10000-0xFFFFFFFF: 5 bytes, 0xFE prefix + 4 bytes little-endian uint32
  • 0x100000000-0xFFFFFFFFFFFFFFFF: 9 bytes, 0xFF prefix + 8 bytes little-endian uint64
§Arguments
  • val - The value to encode
§Example
use bsv_rs::primitives::encoding::Writer;

let mut w = Writer::new();
w.write_var_int(252); // 0xFC - single byte
assert_eq!(w.len(), 1);

let mut w = Writer::new();
w.write_var_int(253); // 0xFD - needs 3-byte encoding
assert_eq!(w.len(), 3);

let mut w = Writer::new();
w.write_var_int(0x10000); // needs 5-byte encoding
assert_eq!(w.len(), 5);

let mut w = Writer::new();
w.write_var_int(0x100000000); // needs 9-byte encoding
assert_eq!(w.len(), 9);
Source

pub fn write_bytes(&mut self, data: &[u8]) -> &mut Self

Writes raw bytes.

§Arguments
  • data - The bytes to write
Source

pub fn write_var_bytes(&mut self, data: &[u8]) -> &mut Self

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

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

§Arguments
  • data - The bytes to write

Trait Implementations§

Source§

impl Clone for Writer

Source§

fn clone(&self) -> Writer

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 Debug for Writer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Writer

Source§

fn default() -> Writer

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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