# Struct encoding::Writer

> Appends canonical encodings to a buffer. Variable fields take their limit and fail above it, mirroring `super::Reader`.

`zaun_community_protocol::encoding::Writer` · Struct

```rust
pub struct Writer { … }
```

Appends canonical encodings to a buffer. Variable fields take their limit and fail above it,
mirroring `super::Reader`.

## Methods

### `new`

```rust
pub fn new() -> Self
```

An empty writer.

### `into_bytes`

```rust
pub fn into_bytes(self) -> Vec<u8>
```

The encoding so far.

### `u8`

```rust
pub fn u8(&mut self, value: u8)
```

`u8`.

### `u16`

```rust
pub fn u16(&mut self, value: u16)
```

`u16`, big-endian.

### `u32`

```rust
pub fn u32(&mut self, value: u32)
```

`u32`, big-endian.

### `u64`

```rust
pub fn u64(&mut self, value: u64)
```

`u64`, big-endian.

### `bool`

```rust
pub fn bool(&mut self, value: bool)
```

`bool`: `0x00` or `0x01`.

### `fixed`

```rust
pub fn fixed(&mut self, bytes: &[u8])
```

`bytes[N]`: the bytes, no prefix.

### `bytes`

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

Variable `bytes`: `u32` length, then the bytes.

# Errors
`EncodeError::OverLimit` when longer than `max`.

### `string`

```rust
pub fn string(&mut self, text: &str, max: usize) -> Result<(), EncodeError>
```

`string`: `u32` byte length, then UTF-8. `max` is in bytes.

# Errors
`EncodeError::OverLimit` when longer than `max` bytes.

### `option`

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

`option<T>`: `0x00`, or `0x01` then the value written by `f`.

# Errors
Whatever `f` returns.

### `list`

```rust
pub fn list<T, impl FnMut(&mut Self, &T) -> Result<(), EncodeError>>(&mut self, items: &[T], max: usize, f: impl FnMut(&mut Self, &T) -> Result<(), EncodeError>) -> Result<(), EncodeError>
```

`list<T>`: `u32` count, then each element written by `f`.

# Errors
`EncodeError::OverLimit` when more than `max` elements, or whatever `f` returns.

### `put`

```rust
pub fn put<T>(&mut self, value: &T) -> Result<(), EncodeError>
```

Writes a value with a canonical encoding.

# Errors
Whatever the value's encoder returns.

## Trait implementations

`Clone`, `Debug`, `Default`

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