Module std::encoding::binary

Fixed-width integer encoding and decoding in little-endian and big-endian byte order.

Mirrors the semantics of Go's encoding/binary package. All integers are represented as i64 at the Hew API boundary: unsigned widths (u16/u32/u64) carry the bit-pattern without sign extension; signed widths (i16/i32/i64) use two's-complement representation.

The put_* functions return a new bytes buffer containing the encoded integer. Callers that need to concatenate multiple values build each buffer separately; a future bytes::append primitive will enable zero-copy assembly once it is available.

The get_* functions read from offset 0 of the supplied buffer; the buffer must contain at least the required number of bytes or the runtime aborts with an out-of-bounds diagnostic.

Examples

import std::encoding::binary;

fn main() {
    let buf = binary.put_u32_le(0x01020304);
    // buf contains [0x04, 0x03, 0x02, 0x01]
    let v = binary.get_u32_le(buf);
    println(v);   // 16909060 (= 0x01020304)
}

Contents

Functions

Function put_u16_le

pub fn put_u16_le(v: i64) -> bytes

Encode a 16-bit value v as 2 little-endian bytes.

Only the low 16 bits of v are written; the upper bits are ignored.

Function put_u32_le

pub fn put_u32_le(v: i64) -> bytes

Encode a 32-bit value v as 4 little-endian bytes.

Only the low 32 bits of v are written; the upper bits are ignored.

Function put_u64_le

pub fn put_u64_le(v: i64) -> bytes

Encode a 64-bit value v as 8 little-endian bytes.

Function put_u16_be

pub fn put_u16_be(v: i64) -> bytes

Encode a 16-bit value v as 2 big-endian bytes.

Only the low 16 bits of v are written; the upper bits are ignored.

Function put_u32_be

pub fn put_u32_be(v: i64) -> bytes

Encode a 32-bit value v as 4 big-endian bytes.

Only the low 32 bits of v are written; the upper bits are ignored.

Function put_u64_be

pub fn put_u64_be(v: i64) -> bytes

Encode a 64-bit value v as 8 big-endian bytes.

Function put_i16_le

pub fn put_i16_le(v: i64) -> bytes

Encode a signed 16-bit value as 2 little-endian bytes.

Function put_i32_le

pub fn put_i32_le(v: i64) -> bytes

Encode a signed 32-bit value as 4 little-endian bytes.

Function put_i64_le

pub fn put_i64_le(v: i64) -> bytes

Encode a signed 64-bit value as 8 little-endian bytes.

Function put_i16_be

pub fn put_i16_be(v: i64) -> bytes

Encode a signed 16-bit value as 2 big-endian bytes.

Function put_i32_be

pub fn put_i32_be(v: i64) -> bytes

Encode a signed 32-bit value as 4 big-endian bytes.

Function put_i64_be

pub fn put_i64_be(v: i64) -> bytes

Encode a signed 64-bit value as 8 big-endian bytes.

Function get_u16_le

pub fn get_u16_le(buf: bytes) -> i64

Decode 2 bytes from offset 0 of buf as an unsigned 16-bit little-endian integer. Returns a value in [0, 65535].

Aborts if buf.len() < 2.

Function get_u32_le

pub fn get_u32_le(buf: bytes) -> i64

Decode 4 bytes from offset 0 of buf as an unsigned 32-bit little-endian integer. Returns a value in [0, 4294967295].

Aborts if buf.len() < 4.

Function get_u64_le

pub fn get_u64_le(buf: bytes) -> i64

Decode 8 bytes from offset 0 of buf as a 64-bit little-endian integer.

Aborts if buf.len() < 8.

Function get_u16_be

pub fn get_u16_be(buf: bytes) -> i64

Decode 2 bytes from offset 0 of buf as an unsigned 16-bit big-endian integer. Returns a value in [0, 65535].

Aborts if buf.len() < 2.

Function get_u32_be

pub fn get_u32_be(buf: bytes) -> i64

Decode 4 bytes from offset 0 of buf as an unsigned 32-bit big-endian integer. Returns a value in [0, 4294967295].

Aborts if buf.len() < 4.

Function get_u64_be

pub fn get_u64_be(buf: bytes) -> i64

Decode 8 bytes from offset 0 of buf as a 64-bit big-endian integer.

Aborts if buf.len() < 8.

Function get_i16_le

pub fn get_i16_le(buf: bytes) -> i64

Decode 2 bytes as a signed 16-bit little-endian integer (two's complement).

Aborts if buf.len() < 2.

Function get_i32_le

pub fn get_i32_le(buf: bytes) -> i64

Decode 4 bytes as a signed 32-bit little-endian integer (two's complement).

Aborts if buf.len() < 4.

Function get_i64_le

pub fn get_i64_le(buf: bytes) -> i64

Decode 8 bytes as a signed 64-bit little-endian integer (two's complement).

i64 is already signed, so no sign extension is required.

Aborts if buf.len() < 8.

Function get_i16_be

pub fn get_i16_be(buf: bytes) -> i64

Decode 2 bytes as a signed 16-bit big-endian integer (two's complement).

Aborts if buf.len() < 2.

Function get_i32_be

pub fn get_i32_be(buf: bytes) -> i64

Decode 4 bytes as a signed 32-bit big-endian integer (two's complement).

Aborts if buf.len() < 4.

Function get_i64_be

pub fn get_i64_be(buf: bytes) -> i64

Decode 8 bytes as a signed 64-bit big-endian integer (two's complement).

Aborts if buf.len() < 8.