Module postcard::ser_flavors

source ·
Expand description

Serialization Flavors

“Flavors” in postcard are used as modifiers to the serialization or deserialization process. Flavors typically modify one or both of the following:

  1. The output medium of the serialization, e.g. whether the data is serialized to a [u8] slice, or a heapless::Vec.
  2. The format of the serialization, such as encoding the serialized output in a COBS format, performing CRC32 checksumming while serializing, etc.

Flavors are implemented using the Flavor trait, which acts as a “middleware” for receiving the bytes as serialized by serde. Multiple flavors may be combined to obtain a desired combination of behavior and storage. When flavors are combined, it is expected that the storage flavor (such as Slice or HVec) is the innermost flavor.

Custom flavors may be defined by users of the postcard crate, however some commonly useful flavors have been provided in this module. If you think your custom flavor would be useful to others, PRs adding flavors are very welcome!


Flavors may not always be convenient to use directly, as they may expose some implementation details of how the inner workings of the flavor behaves. It is typical to provide a convenience method for using a flavor, to prevent the user from having to specify generic parameters, setting correct initialization values, or handling the output of the flavor correctly. See postcard::to_vec() for an example of this.

It is recommended to use the serialize_with_flavor() method for serialization. See it’s documentation for information regarding usage and generic type parameters.

When to use (multiple) flavors

Combining flavors are nice for convenience, as they perform potentially multiple steps of serialization at one time.

This can often be more memory efficient, as intermediate buffers are not typically required.

When NOT to use (multiple) flavors

The downside of passing serialization through multiple steps is that it is typically slower than performing each step serially. Said simply, “cobs encoding while serializing” is often slower than “serialize then cobs encode”, due to the ability to handle longer “runs” of data in each stage. The downside is that if these stages can not be performed in-place on the buffer, you will need additional buffers for each stage.


Using a single flavor

In the first example, we use the Slice flavor, to store the serialized output into a mutable [u8] slice. No other modification is made to the serialization process.

use postcard::{

let mut buf = [0u8; 32];

let data: &[u8] = &[0x01, 0x00, 0x20, 0x30];
let buffer = &mut [0u8; 32];
let res = serialize_with_flavor::<[u8], Slice, &mut [u8]>(

assert_eq!(res, &[0x04, 0x01, 0x00, 0x20, 0x30]);

Using combined flavors

In the second example, we mix Slice with Cobs, to cobs encode the output while the data is serialized. Notice how Slice (the storage flavor) is the innermost flavor used.

use postcard::{
    ser_flavors::{Cobs, Slice},

let mut buf = [0u8; 32];

let data: &[u8] = &[0x01, 0x00, 0x20, 0x30];
let buffer = &mut [0u8; 32];
let res = serialize_with_flavor::<[u8], Cobs<Slice>, &mut [u8]>(

assert_eq!(res, &[0x03, 0x04, 0x01, 0x03, 0x20, 0x30, 0x00]);


The AllocVec flavor is a wrapper type around an alloc::vec::Vec.
The Cobs flavor implements Consistent Overhead Byte Stuffing on the serialized data. The output of this flavor includes the termination/sentinel byte of 0x00.
The HVec flavor is a wrapper type around a heapless::Vec. This is a stack allocated data structure, with a fixed maximum size and variable amount of contents.
The Size flavor is a measurement flavor, which accumulates the number of bytes needed to serialize the data.
The Slice flavor is a storage flavor, storing the serialized (or otherwise modified) bytes into a plain [u8] slice. The Slice flavor resolves into a sub-slice of the original slice buffer.


The serialization Flavor trait

Type Definitions

The StdVec flavor is a wrapper type around a std::vec::Vec.