Skip to main content

Reader

Struct Reader 

Source
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>

Source

pub const fn new(buf: &'a [u8]) -> Self

A reader over buf, with offsets reported from its start.

Source

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.

Source

pub const fn remaining(&self) -> usize

Bytes not yet consumed.

Source

pub const fn is_empty(&self) -> bool

True when every byte has been consumed.

Source

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.

Source

pub fn rest(&self) -> &'a [u8] ⓘ

The bytes not yet consumed, without consuming them.

Source

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.

Source

pub const fn err(&self, kind: ErrorKind) -> Error

An error at the current position.

Source

pub const fn err_back(&self, kind: ErrorKind, back: usize) -> Error

An error at offset bytes before the current position.

Source

pub fn ensure(&self, n: usize) -> Result<()>

Fail unless at least n bytes remain.

Source

pub fn take(&mut self, n: usize) -> Result<&'a [u8]>

Take exactly n bytes.

Source

pub fn take_rest(&mut self) -> &'a [u8] ⓘ

Take the remaining bytes.

Source

pub fn array<const N: usize>(&mut self) -> Result<[u8; N]>

Take exactly N bytes as an array.

Source

pub fn u8(&mut self) -> Result<u8>

One byte.

Source

pub fn peek_u8(&self) -> Result<u8>

One byte, without consuming it.

Source

pub fn u16(&mut self) -> Result<u16>

A big-endian u16. DLMS is big-endian throughout.

Source

pub fn u32(&mut self) -> Result<u32>

A big-endian u32.

Source

pub fn u64(&mut self) -> Result<u64>

A big-endian u64.

Source

pub fn i8(&mut self) -> Result<i8>

A big-endian i8, i16, i32, i64 — sign is the only difference.

Source

pub fn i16(&mut self) -> Result<i16>

A big-endian i16.

Source

pub fn i32(&mut self) -> Result<i32>

A big-endian i32.

Source

pub fn i64(&mut self) -> Result<i64>

A big-endian i64.

Source

pub fn skip(&mut self, n: usize) -> Result<()>

Skip n bytes.

Source

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.

Source

pub fn length_prefixed(&mut self) -> Result<&'a [u8]>

A length followed by that many bytes.

Source

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.

Trait Implementations§

Source§

impl<'a> Clone for Reader<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Reader<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Forget for Reader<'a>

§

impl<'a> Freeze for Reader<'a>
where &'a [u8]: Freeze,

§

impl<'a> Move for Reader<'a>

§

impl<'a> RefUnwindSafe for Reader<'a>

§

impl<'a> Send for Reader<'a>
where &'a [u8]: Send,

§

impl<'a> Sync for Reader<'a>

§

impl<'a> Unpin for Reader<'a>
where &'a [u8]: Unpin,

§

impl<'a> UnsafeUnpin for Reader<'a>
where &'a [u8]: UnsafeUnpin,

§

impl<'a> UnwindSafe for Reader<'a>
where &'a [u8]: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.