ByteReader
struct ByteReader[origin: Origin[mut=origin.mut]]Bounds-checked cursor over a borrowed byte span.
Holds the borrowed buf and a read position pos. Every
read_* advances pos and raises Error if the buffer is
too short, so a malformed/truncated input can never read out of
bounds. read_utf8 additionally validates UTF-8.
Implements Movable
- Fields
buf: Span[UInt8, origin]: Borrowed source buffer. Lifetime tied toorigin.pos: Int: Number of bytes already consumed from the front ofbuf.
__init__
def __init__(out self, buf: Span[UInt8, origin]) Construct a reader positioned at the start of buf.
remaining
def remaining(self) -> Int Bytes left to read (len(buf) - pos).
position
def position(self) -> Int Current read offset from the start of buf.
read_u8
def read_u8(mut self) -> UInt8 Read one byte.
read_u16_be
def read_u16_be(mut self) -> UInt16 Read a big-endian u16.
read_u16_le
def read_u16_le(mut self) -> UInt16 Read a little-endian u16.
read_u32_be
def read_u32_be(mut self) -> UInt32 Read a big-endian u32.
read_u32_le
def read_u32_le(mut self) -> UInt32 Read a little-endian u32.
read_u64_be
def read_u64_be(mut self) -> UInt64 Read a big-endian u64.
read_u64_le
def read_u64_le(mut self) -> UInt64 Read a little-endian u64.
read_bytes
def read_bytes(mut self, n: Int) -> Span[UInt8, origin] Borrow the next n bytes as a sub-span (no copy).
The returned span shares origin with this reader's buffer.
read_utf8
def read_utf8(mut self, n: Int) -> String Read n bytes and return them as a validated String.
Raises if fewer than n bytes remain or the bytes are not
well-formed UTF-8.
skip
def skip(mut self, n: Int) Advance the cursor by n bytes without reading them.