std::encoding::binaryFixed-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.
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)
}
put_u16_leEncode a 16-bit value v as 2 little-endian bytes.
Only the low 16 bits of v are written; the upper bits are ignored.
put_u32_leEncode a 32-bit value v as 4 little-endian bytes.
Only the low 32 bits of v are written; the upper bits are ignored.
put_u64_leEncode a 64-bit value v as 8 little-endian bytes.
put_u16_beEncode a 16-bit value v as 2 big-endian bytes.
Only the low 16 bits of v are written; the upper bits are ignored.
put_u32_beEncode a 32-bit value v as 4 big-endian bytes.
Only the low 32 bits of v are written; the upper bits are ignored.
put_u64_beEncode a 64-bit value v as 8 big-endian bytes.
put_i16_leEncode a signed 16-bit value as 2 little-endian bytes.
put_i32_leEncode a signed 32-bit value as 4 little-endian bytes.
put_i64_leEncode a signed 64-bit value as 8 little-endian bytes.
put_i16_beEncode a signed 16-bit value as 2 big-endian bytes.
put_i32_beEncode a signed 32-bit value as 4 big-endian bytes.
put_i64_beEncode a signed 64-bit value as 8 big-endian bytes.
get_u16_leDecode 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.
get_u32_leDecode 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.
get_u64_leDecode 8 bytes from offset 0 of buf as a 64-bit little-endian integer.
Aborts if buf.len() < 8.
get_u16_beDecode 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.
get_u32_beDecode 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.
get_u64_beDecode 8 bytes from offset 0 of buf as a 64-bit big-endian integer.
Aborts if buf.len() < 8.
get_i16_leDecode 2 bytes as a signed 16-bit little-endian integer (two's complement).
Aborts if buf.len() < 2.
get_i32_leDecode 4 bytes as a signed 32-bit little-endian integer (two's complement).
Aborts if buf.len() < 4.
get_i64_leDecode 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.
get_i16_beDecode 2 bytes as a signed 16-bit big-endian integer (two's complement).
Aborts if buf.len() < 2.
get_i32_beDecode 4 bytes as a signed 32-bit big-endian integer (two's complement).
Aborts if buf.len() < 4.
get_i64_beDecode 8 bytes as a signed 64-bit big-endian integer (two's complement).
Aborts if buf.len() < 8.