Binary encoding v0.1.0

Source
Fixed-width integers and floats read from a reader and written to a writer in a chosen byte order.

Endian

pub enum Endian: int32 {
    Little = 0,
    Big = 1
}

A byte order: which end of a multi-byte value comes first.

Source

Cases

NameValueDescription
Little0The least significant byte first: x86-64, AArch64, and most formats born on them.
Big1The most significant byte first: network order, and the older big machines.

ReadWord

pub func ReadWord(
    reader: &var Reader,
    endian: Endian,
    width: uint
) -> uint512 ! IoError

Reads a width-byte unsigned word in the given order, into the low bytes of the result. The word functions are the one implementation the typed surface below stands on; call those unless the width itself is a runtime quantity.

Source

Parameters

NameTypeDescription
reader&var Readerwhere the bytes come from
endianEndianwhich byte order the value is stored in
widthuinthow many bytes the value occupies

Returns: the value read, in the low bytes; IsEnd when the stream ended first

WriteWord

pub func WriteWord(
    writer: &var Writer,
    endian: Endian,
    width: uint,
    value: uint512
) -> ! IoError

Writes the low width bytes of value in the given order.

Source

Parameters

NameTypeDescription
writer&var Writerthe destination
endianEndianwhich byte order the value is stored in
widthuinthow many bytes the value occupies
valueuint512the value to write

Returns: success when the whole value was written

ReadUint8

pub func ReadUint8(
    reader: &var Reader,
    endian: Endian
) -> uint8 ! IoError

Reads a uint8 in the given order.

Source

Parameters

NameTypeDescription
reader&var Readerwhere the bytes come from
endianEndianwhich byte order the value is stored in

Returns: the value read; IsEnd when the stream ended first

WriteUint8

pub func WriteUint8(
    writer: &var Writer,
    endian: Endian,
    value: uint8
) -> ! IoError

Writes a uint8 in the given order.

Source

Parameters

NameTypeDescription
writer&var Writerthe destination
endianEndianwhich byte order the value is stored in
valueuint8the value to write

Returns: success when the whole value was written

ReadInt8

pub func ReadInt8(
    reader: &var Reader,
    endian: Endian
) -> int8 ! IoError

Reads an int8: the unsigned bytes, reinterpreted in two's complement.

Source

Parameters

NameTypeDescription
reader&var Readerwhere the bytes come from
endianEndianwhich byte order the value is stored in

Returns: the value read; IsEnd when the stream ended first

WriteInt8

pub func WriteInt8(
    writer: &var Writer,
    endian: Endian,
    value: int8
) -> ! IoError

Writes an int8: its two's-complement bytes, unchanged.

Source

Parameters

NameTypeDescription
writer&var Writerthe destination
endianEndianwhich byte order the value is stored in
valueint8the value to write

Returns: success when the whole value was written

ReadUint16

pub func ReadUint16(
    reader: &var Reader,
    endian: Endian
) -> uint16 ! IoError

Reads a uint16 in the given order.

Source

Parameters

NameTypeDescription
reader&var Readerwhere the bytes come from
endianEndianwhich byte order the value is stored in

Returns: the value read; IsEnd when the stream ended first

WriteUint16

pub func WriteUint16(
    writer: &var Writer,
    endian: Endian,
    value: uint16
) -> ! IoError

Writes a uint16 in the given order.

Source

Parameters

NameTypeDescription
writer&var Writerthe destination
endianEndianwhich byte order the value is stored in
valueuint16the value to write

Returns: success when the whole value was written

ReadInt16

pub func ReadInt16(
    reader: &var Reader,
    endian: Endian
) -> int16 ! IoError

Reads an int16: the unsigned bytes, reinterpreted in two's complement.

Source

Parameters

NameTypeDescription
reader&var Readerwhere the bytes come from
endianEndianwhich byte order the value is stored in

Returns: the value read; IsEnd when the stream ended first

WriteInt16

pub func WriteInt16(
    writer: &var Writer,
    endian: Endian,
    value: int16
) -> ! IoError

Writes an int16: its two's-complement bytes, unchanged.

Source

Parameters

NameTypeDescription
writer&var Writerthe destination
endianEndianwhich byte order the value is stored in
valueint16the value to write

Returns: success when the whole value was written

ReadUint32

pub func ReadUint32(
    reader: &var Reader,
    endian: Endian
) -> uint32 ! IoError

Reads a uint32 in the given order.

Source

Parameters

NameTypeDescription
reader&var Readerwhere the bytes come from
endianEndianwhich byte order the value is stored in

Returns: the value read; IsEnd when the stream ended first

WriteUint32

pub func WriteUint32(
    writer: &var Writer,
    endian: Endian,
    value: uint32
) -> ! IoError

Writes a uint32 in the given order.

Source

Parameters

NameTypeDescription
writer&var Writerthe destination
endianEndianwhich byte order the value is stored in
valueuint32the value to write

Returns: success when the whole value was written

ReadInt32

pub func ReadInt32(
    reader: &var Reader,
    endian: Endian
) -> int32 ! IoError

Reads an int32: the unsigned bytes, reinterpreted in two's complement.

Source

Parameters

NameTypeDescription
reader&var Readerwhere the bytes come from
endianEndianwhich byte order the value is stored in

Returns: the value read; IsEnd when the stream ended first

WriteInt32

pub func WriteInt32(
    writer: &var Writer,
    endian: Endian,
    value: int32
) -> ! IoError

Writes an int32: its two's-complement bytes, unchanged.

Source

Parameters

NameTypeDescription
writer&var Writerthe destination
endianEndianwhich byte order the value is stored in
valueint32the value to write

Returns: success when the whole value was written

ReadUint64

pub func ReadUint64(
    reader: &var Reader,
    endian: Endian
) -> uint64 ! IoError

Reads a uint64 in the given order.

Source

Parameters

NameTypeDescription
reader&var Readerwhere the bytes come from
endianEndianwhich byte order the value is stored in

Returns: the value read; IsEnd when the stream ended first

WriteUint64

pub func WriteUint64(
    writer: &var Writer,
    endian: Endian,
    value: uint64
) -> ! IoError

Writes a uint64 in the given order.

Source

Parameters

NameTypeDescription
writer&var Writerthe destination
endianEndianwhich byte order the value is stored in
valueuint64the value to write

Returns: success when the whole value was written

ReadInt64

pub func ReadInt64(
    reader: &var Reader,
    endian: Endian
) -> int64 ! IoError

Reads an int64: the unsigned bytes, reinterpreted in two's complement.

Source

Parameters

NameTypeDescription
reader&var Readerwhere the bytes come from
endianEndianwhich byte order the value is stored in

Returns: the value read; IsEnd when the stream ended first

WriteInt64

pub func WriteInt64(
    writer: &var Writer,
    endian: Endian,
    value: int64
) -> ! IoError

Writes an int64: its two's-complement bytes, unchanged.

Source

Parameters

NameTypeDescription
writer&var Writerthe destination
endianEndianwhich byte order the value is stored in
valueint64the value to write

Returns: success when the whole value was written

ReadUint128

pub func ReadUint128(
    reader: &var Reader,
    endian: Endian
) -> uint128 ! IoError

Reads a uint128 in the given order.

Source

Parameters

NameTypeDescription
reader&var Readerwhere the bytes come from
endianEndianwhich byte order the value is stored in

Returns: the value read; IsEnd when the stream ended first

WriteUint128

pub func WriteUint128(
    writer: &var Writer,
    endian: Endian,
    value: uint128
) -> ! IoError

Writes a uint128 in the given order.

Source

Parameters

NameTypeDescription
writer&var Writerthe destination
endianEndianwhich byte order the value is stored in
valueuint128the value to write

Returns: success when the whole value was written

ReadInt128

pub func ReadInt128(
    reader: &var Reader,
    endian: Endian
) -> int128 ! IoError

Reads an int128: the unsigned bytes, reinterpreted in two's complement.

Source

Parameters

NameTypeDescription
reader&var Readerwhere the bytes come from
endianEndianwhich byte order the value is stored in

Returns: the value read; IsEnd when the stream ended first

WriteInt128

pub func WriteInt128(
    writer: &var Writer,
    endian: Endian,
    value: int128
) -> ! IoError

Writes an int128: its two's-complement bytes, unchanged.

Source

Parameters

NameTypeDescription
writer&var Writerthe destination
endianEndianwhich byte order the value is stored in
valueint128the value to write

Returns: success when the whole value was written

ReadUint256

pub func ReadUint256(
    reader: &var Reader,
    endian: Endian
) -> uint256 ! IoError

Reads a uint256 in the given order.

Source

Parameters

NameTypeDescription
reader&var Readerwhere the bytes come from
endianEndianwhich byte order the value is stored in

Returns: the value read; IsEnd when the stream ended first

WriteUint256

pub func WriteUint256(
    writer: &var Writer,
    endian: Endian,
    value: uint256
) -> ! IoError

Writes a uint256 in the given order.

Source

Parameters

NameTypeDescription
writer&var Writerthe destination
endianEndianwhich byte order the value is stored in
valueuint256the value to write

Returns: success when the whole value was written

ReadInt256

pub func ReadInt256(
    reader: &var Reader,
    endian: Endian
) -> int256 ! IoError

Reads an int256: the unsigned bytes, reinterpreted in two's complement.

Source

Parameters

NameTypeDescription
reader&var Readerwhere the bytes come from
endianEndianwhich byte order the value is stored in

Returns: the value read; IsEnd when the stream ended first

WriteInt256

pub func WriteInt256(
    writer: &var Writer,
    endian: Endian,
    value: int256
) -> ! IoError

Writes an int256: its two's-complement bytes, unchanged.

Source

Parameters

NameTypeDescription
writer&var Writerthe destination
endianEndianwhich byte order the value is stored in
valueint256the value to write

Returns: success when the whole value was written

ReadUint512

pub func ReadUint512(
    reader: &var Reader,
    endian: Endian
) -> uint512 ! IoError

Reads a uint512 in the given order.

Source

Parameters

NameTypeDescription
reader&var Readerwhere the bytes come from
endianEndianwhich byte order the value is stored in

Returns: the value read; IsEnd when the stream ended first

WriteUint512

pub func WriteUint512(
    writer: &var Writer,
    endian: Endian,
    value: uint512
) -> ! IoError

Writes a uint512 in the given order.

Source

Parameters

NameTypeDescription
writer&var Writerthe destination
endianEndianwhich byte order the value is stored in
valueuint512the value to write

Returns: success when the whole value was written

ReadInt512

pub func ReadInt512(
    reader: &var Reader,
    endian: Endian
) -> int512 ! IoError

Reads an int512: the unsigned bytes, reinterpreted in two's complement.

Source

Parameters

NameTypeDescription
reader&var Readerwhere the bytes come from
endianEndianwhich byte order the value is stored in

Returns: the value read; IsEnd when the stream ended first

WriteInt512

pub func WriteInt512(
    writer: &var Writer,
    endian: Endian,
    value: int512
) -> ! IoError

Writes an int512: its two's-complement bytes, unchanged.

Source

Parameters

NameTypeDescription
writer&var Writerthe destination
endianEndianwhich byte order the value is stored in
valueint512the value to write

Returns: success when the whole value was written

ReadFloat32

pub func ReadFloat32(
    reader: &var Reader,
    endian: Endian
) -> float32 ! IoError

Reads a float32 as the IEEE 754 bit pattern of its width.

Source

Parameters

NameTypeDescription
reader&var Readerwhere the bytes come from
endianEndianwhich byte order the value is stored in

Returns: the value read; IsEnd when the stream ended first

WriteFloat32

pub func WriteFloat32(
    writer: &var Writer,
    endian: Endian,
    value: float32
) -> ! IoError

Writes a float32 as the IEEE 754 bit pattern of its width.

Source

Parameters

NameTypeDescription
writer&var Writerthe destination
endianEndianwhich byte order the value is stored in
valuefloat32the value to write

Returns: success when the whole value was written

ReadFloat64

pub func ReadFloat64(
    reader: &var Reader,
    endian: Endian
) -> float64 ! IoError

Reads a float64 as the IEEE 754 bit pattern of its width.

Source

Parameters

NameTypeDescription
reader&var Readerwhere the bytes come from
endianEndianwhich byte order the value is stored in

Returns: the value read; IsEnd when the stream ended first

WriteFloat64

pub func WriteFloat64(
    writer: &var Writer,
    endian: Endian,
    value: float64
) -> ! IoError

Writes a float64 as the IEEE 754 bit pattern of its width.

Source

Parameters

NameTypeDescription
writer&var Writerthe destination
endianEndianwhich byte order the value is stored in
valuefloat64the value to write

Returns: success when the whole value was written