#serde #framing #cobs

no-std postcard

A no_std + serde compatible message library for Rust

10 releases (4 breaking)

✓ Uses Rust 2018 edition

0.4.1 Sep 9, 2019
0.4.0 Aug 12, 2019
0.3.2 Apr 14, 2019
0.2.1 Apr 8, 2019
0.0.1 Apr 5, 2019

#25 in No standard library

Download history 74/week @ 2019-07-01 61/week @ 2019-07-08 29/week @ 2019-07-15 72/week @ 2019-07-22 68/week @ 2019-07-29 73/week @ 2019-08-05 35/week @ 2019-08-12 86/week @ 2019-08-19 185/week @ 2019-08-26 83/week @ 2019-09-02 66/week @ 2019-09-09 105/week @ 2019-09-16 46/week @ 2019-09-23 75/week @ 2019-09-30 47/week @ 2019-10-07

327 downloads per month
Used in 2 crates


1.5K SLoC


Postcard is a #![no_std] focused serializer and deserializer for Serde.

Postcard aims to be convenient for developers in constrained environments, while allowing for flexibility to customize behavior as needed.

Design Goals

  1. Design primarily for #![no_std] usage, in embedded or other constrained contexts
  2. Support a maximal set of serde features, so postcard can be used as a drop in replacement
  3. Avoid special differences in code between communication code written for a microcontroller or a desktop/server PC
  4. Be resource efficient - memory usage, code size, developer time, and CPU time; in that order
  5. Allow library users to customize the serialization and deserialization behavior to fit their bespoke needs

Variable Length Data

Variable length data (such as slices) are prefixed by their length.

Length is encoded as a Varint. This is done for two reasons: to minimize wasted bytes on the wire when sending slices with items less than 127 items (typical for embedded), and to reduce compatibility issues between 32-bit and 64-bit targets due to differing sizes of usize.

Similarly, enum descriminants are encoded as varints, meaning that any enum with less than 127 variants will encode its discriminant as a single byte (rather than a u32).

Varints in postcard have a maximum value of the usize for that platform. In practice, this means that 64-bit targets should not send messages with slices containing (2 << 32) -1 items to 32-bit targets, which is uncommon in practice. Enum discriminants already have a fixed maximum value of (2 << 32) - 1 as currently defined in Rust. Varints larger than the current platform's usize will cause the deserialization process to return an Err.

Example - Serialization/Deserialization

Postcard can serialize and deserialize messages similar to other serde formats.

use core::ops::Deref;
use serde::{Serialize, Deserialize};
use postcard::{from_bytes, to_vec};
use heapless::{Vec, consts::*};

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
struct RefStruct<'a> {
    bytes: &'a [u8],
    str_s: &'a str,
let message = "hElLo";
let bytes = [0x01, 0x10, 0x02, 0x20];
let output: Vec<u8, U11> = to_vec(&RefStruct {
    bytes: &bytes,
    str_s: message,

    &[0x04, 0x01, 0x10, 0x02, 0x20, 0x05, b'h', b'E', b'l', b'L', b'o',],

let out: RefStruct = from_bytes(output.deref()).unwrap();
    RefStruct {
        bytes: &bytes,
        str_s: message,

Example - Flavors

postcard supports a system called Flavors, which are used to modify the way postcard serializes or processes serialized data. These flavors act as "plugins" or "middlewares" during the serialization process, and can be combined to obtain complex protocol formats.

Here, we serialize the given data, while simultaneously encoding it using COBS (a "modification flavor"), and placing the output in a byte slice (a "storage flavor").

Users of postcard can define their own Flavors that can be combined with existing Flavors.

use postcard::{
    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]);


Licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


~39K SLoC