ByteReader
A cursor over a byte slice, for decoding binary formats: fixed-size integers and floats, LEB128 varints (signed ones zigzag-encoded), byte runs and NUL-terminated strings.
Every read_* method returns None when the data is truncated or invalid, and then leaves
the position where it was, so position is the offset of the read that
failed: enough to build a precise error in the caller’s own error type.
Import
Section titled “Import”Cargo feature bytes (enabled by default). To compile only this module:
or in Cargo.toml:
Definition
Section titled “Definition”Examples
Section titled “Examples”Methods
Section titled “Methods”read_u8
Section titled “read_u8”Reads one byte.
Returns
Option<u8> — The byte, or None at the end of the data.
Examples
read_bytes
Section titled “read_bytes”Reads len bytes, borrowed from the data (no copy).
Parameters
| Parameter | Type | Description |
|---|---|---|
len | usize | The number of bytes. |
Returns
Option<&'a [u8]> — The bytes, or None when fewer than len remain.
Examples
read_u16
Section titled “read_u16”Reads a u16 in the given byte order.
Parameters
| Parameter | Type | Description |
|---|---|---|
endian | Endian | The byte order of the value. |
Returns
Option<u16> — The value, or None when fewer than 2 bytes remain.
Examples
read_u32
Section titled “read_u32”Reads a u32 in the given byte order.
Parameters
| Parameter | Type | Description |
|---|---|---|
endian | Endian | The byte order of the value. |
Returns
Option<u32> — The value, or None when fewer than 4 bytes remain.
Examples
read_u64
Section titled “read_u64”Reads a u64 in the given byte order.
Parameters
| Parameter | Type | Description |
|---|---|---|
endian | Endian | The byte order of the value. |
Returns
Option<u64> — The value, or None when fewer than 8 bytes remain.
Examples
read_i16
Section titled “read_i16”Reads an i16 (two’s complement) in the given byte order.
Parameters
| Parameter | Type | Description |
|---|---|---|
endian | Endian | The byte order of the value. |
Returns
Option<i16> — The value, or None when fewer than 2 bytes remain.
Examples
read_i32
Section titled “read_i32”Reads an i32 (two’s complement) in the given byte order.
Parameters
| Parameter | Type | Description |
|---|---|---|
endian | Endian | The byte order of the value. |
Returns
Option<i32> — The value, or None when fewer than 4 bytes remain.
Examples
read_i64
Section titled “read_i64”Reads an i64 (two’s complement) in the given byte order.
Parameters
| Parameter | Type | Description |
|---|---|---|
endian | Endian | The byte order of the value. |
Returns
Option<i64> — The value, or None when fewer than 8 bytes remain.
Examples
read_f32
Section titled “read_f32”Reads an IEEE 754 f32 in the given byte order.
Parameters
| Parameter | Type | Description |
|---|---|---|
endian | Endian | The byte order of the value. |
Returns
Option<f32> — The value, or None when fewer than 4 bytes remain.
Examples
read_f64
Section titled “read_f64”Reads an IEEE 754 f64 in the given byte order.
Parameters
| Parameter | Type | Description |
|---|---|---|
endian | Endian | The byte order of the value. |
Returns
Option<f64> — The value, or None when fewer than 8 bytes remain.
Examples
read_var_u32
Section titled “read_var_u32”Reads an unsigned LEB128 varint of at most 32 bits: 7 bits per byte, least significant first, the high bit set on every byte but the last.
Overlong encodings (0x80 0x00 for 0) are accepted, as most decoders do.
Returns
Option<u32> — The value, or None when the data ends inside the varint or the value needs more than
32 bits (a 5th byte above 0x0f, or a 6th byte).
Examples
read_var_u64
Section titled “read_var_u64”Reads an unsigned LEB128 varint of at most 64 bits (up to 10 bytes), like
read_var_u32.
Returns
Option<u64> — The value, or None when the data ends inside the varint or the value needs more than
64 bits (a 10th byte above 0x01, or an 11th byte).
Examples
read_var_i32
Section titled “read_var_i32”Reads a signed varint: a zigzag-encoded (see zigzag_decode) LEB128 varint of at
most 32 bits.
Returns
Option<i32> — The value, or None as for read_var_u32.
Examples
read_var_i64
Section titled “read_var_i64”Reads a signed varint: a zigzag-encoded (see zigzag_decode) LEB128 varint of at
most 64 bits.
Returns
Option<i64> — The value, or None as for read_var_u64.
Examples
read_cstr
Section titled “read_cstr”Reads a NUL-terminated UTF-8 string and skips the terminating 0.
Returns
Option<&'a str> — The string without its terminator, borrowed from the data, or None when there is no
0 byte before the end or the bytes are not valid UTF-8.
Examples
