# Struct encoding::Reader

> Reads canonical encodings from a byte slice. Lengths and counts are checked against the field's limit and the remaining input before anything is allocated.

`zaun_community_protocol::encoding::Reader` · Struct

```rust
pub struct Reader<'a> { … }
```

Reads canonical encodings from a byte slice. Lengths and counts are checked against the
field's limit and the remaining input before anything is allocated.

## Methods

### `bytes`

```rust
pub fn bytes(&mut self, max: usize) -> Result<&'a [u8], DecodeError>
```

Variable `bytes` of at most `max` bytes.

# Errors
`DecodeError::OverLimit` or `DecodeError::Truncated`.

### `string`

```rust
pub fn string(&mut self, max: usize) -> Result<&'a str, DecodeError>
```

`string` of at most `max` bytes; valid UTF-8, not normalised.

# Errors
As `Self::bytes`, or `DecodeError::InvalidUtf8`.

### `option`

```rust
pub fn option<T, impl FnOnce(&mut Self) -> Result<T, DecodeError>>(&mut self, f: impl FnOnce(&mut Self) -> Result<T, DecodeError>) -> Result<Option<T>, DecodeError>
```

`option<T>` with the value read by `f`; tags other than `0x00`/`0x01` are errors.

# Errors
`DecodeError::InvalidOptionTag`, or whatever `f` returns.

### `list`

```rust
pub fn list<T, impl FnMut(&mut Self) -> Result<T, DecodeError>>(&mut self, max: usize, f: impl FnMut(&mut Self) -> Result<T, DecodeError>) -> Result<Vec<T>, DecodeError>
```

`list<T>` of at most `max` elements, each read by `f`.

# Errors
`DecodeError::OverLimit`, `DecodeError::Truncated`, or whatever `f` returns.

### `new`

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

A reader over `input`.

### `remaining`

```rust
pub const fn remaining(&self) -> usize
```

Bytes not yet read.

### `finish`

```rust
pub const fn finish(self) -> Result<(), DecodeError>
```

Ends a top-level decode.

# Errors
`DecodeError::TrailingBytes` when input remains.

### `fixed`

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

`bytes[n]`: the next `n` bytes.

# Errors
`DecodeError::Truncated`.

### `array`

```rust
pub fn array<N>(&mut self) -> Result<[u8; N], DecodeError>
```

`bytes[N]` as an array.

# Errors
`DecodeError::Truncated`.

### `peek_u8`

```rust
pub fn peek_u8(&self) -> Result<u8, DecodeError>
```

The next byte without consuming it.

# Errors
`DecodeError::Truncated`.

### `u8`

```rust
pub fn u8(&mut self) -> Result<u8, DecodeError>
```

`u8`.

# Errors
`DecodeError::Truncated`.

### `u16`

```rust
pub fn u16(&mut self) -> Result<u16, DecodeError>
```

`u16`, big-endian.

# Errors
`DecodeError::Truncated`.

### `u32`

```rust
pub fn u32(&mut self) -> Result<u32, DecodeError>
```

`u32`, big-endian.

# Errors
`DecodeError::Truncated`.

### `u64`

```rust
pub fn u64(&mut self) -> Result<u64, DecodeError>
```

`u64`, big-endian.

# Errors
`DecodeError::Truncated`.

### `bool`

```rust
pub fn bool(&mut self) -> Result<bool, DecodeError>
```

`bool`: only `0x00` and `0x01`.

# Errors
`DecodeError::InvalidBool` or `DecodeError::Truncated`.

### `get`

```rust
pub fn get<T>(&mut self) -> Result<T, DecodeError>
```

Reads a value with a canonical decoding.

# Errors
Whatever the value's decoder returns.

## Trait implementations

`Clone`, `Debug`

[Full rustdoc page](/docs/api/rust/zaun-community-protocol/zaun_community_protocol/encoding/reader/struct.Reader.html)
