pub struct Reader<'a> { /* private fields */ }Expand description
A bounds-checked cursor over borrowed input.
Every method that fails leaves the position untouched, so a caller that hits
ErrorKind::Truncated can retry the same read after more bytes arrive.
Implementations§
Source§impl<'a> Reader<'a>
impl<'a> Reader<'a>
Sourcepub const fn new(buf: &'a [u8]) -> Self
pub const fn new(buf: &'a [u8]) -> Self
A reader over buf, with offsets reported from its start.
Sourcepub const fn with_base(buf: &'a [u8], base: usize) -> Self
pub const fn with_base(buf: &'a [u8], base: usize) -> Self
A reader over buf whose reported offsets are shifted by base.
Used when a sub-decoder is handed a slice out of a larger frame and its errors should still point at the position in that frame.
Sourcepub const fn offset(&self) -> usize
pub const fn offset(&self) -> usize
The current offset, including the base.
Saturating rather than wrapping: this is a byte position for a diagnostic, and
the sum cannot overflow for any buffer that exists. Writing it as a plain +
still emitted an overflow check, and because Reader::err calls this, that
single addition put a panic path in every decoder in the crate.
Sourcepub const fn consumed(&self) -> usize
pub const fn consumed(&self) -> usize
How many bytes this reader has taken, ignoring the base.
This is what a probe reader is for: make one over rest(), run a sub-decoder,
ask how much it used. A list finds its end that way, and so does a compact array’s
type description.
Written probe.offset() - base instead, that is a subtraction the optimiser
cannot discharge — it cannot see that a reader’s offset never goes below its own
base — so it emits an overflow check in every monomorphisation of every such
decoder. Asking the reader needs no arithmetic at all.
Sourcepub const fn err_back(&self, kind: ErrorKind, back: usize) -> Error
pub const fn err_back(&self, kind: ErrorKind, back: usize) -> Error
An error at offset bytes before the current position.
Sourcepub fn i8(&mut self) -> Result<i8>
pub fn i8(&mut self) -> Result<i8>
A big-endian i8, i16, i32, i64 — sign is the only difference.
Sourcepub fn length(&mut self) -> Result<usize>
pub fn length(&mut self) -> Result<usize>
An A-XDR length: a short form below 0x80, else 0x80 | n and n big-endian
bytes. n above four, and the indefinite form 0x80, are refused — DLMS uses
neither and accepting them widens the attack surface for nothing.
Sourcepub fn length_prefixed(&mut self) -> Result<&'a [u8]>
pub fn length_prefixed(&mut self) -> Result<&'a [u8]>
A length followed by that many bytes.
Sourcepub fn nested<T>(
&mut self,
n: usize,
f: impl FnOnce(&mut Reader<'a>) -> Result<T>,
) -> Result<T>
pub fn nested<T>( &mut self, n: usize, f: impl FnOnce(&mut Reader<'a>) -> Result<T>, ) -> Result<T>
Run f over the next n bytes in a reader of their own, and require it to
consume all of them.
This is how a nested structure is decoded without letting the inner decoder read past its own field — the single most common way a protocol parser is made to read out of bounds elsewhere in the frame.