Postcard is a #![no_std] focused serializer and deserializer for Serde.
Caution
This readme is for postcard v2.0.0, which is in release candidates
(2.0.0-rc.N). APIs may still change before the final 2.0.0.
View the v1.x branch
for the README of the current stable release, 1.2.0.
Postcard aims to be convenient for developers in constrained environments, while allowing for flexibility to customize behavior as needed.
- Design primarily for
#![no_std]usage, in embedded or other constrained contexts - Support a maximal set of
serdefeatures, sopostcardcan be used as a drop in replacement - Avoid special differences in code between communication code written for a microcontroller or a desktop/server PC
- Be resource efficient - memory usage, code size, developer time, and CPU time; in that order
- Allow library users to customize the serialization and deserialization behavior to fit their bespoke needs
As of v1.0.0, postcard has a documented and stable wire format. The
specifications are in the spec/ folder of the Postcard repository, and
published at https://postcard.rs:
- Postcard Wire Specification 1.1, which
postcard2.0 implements - Postcard Wire Specification 1.0, the original specification
- Postcard Schema Specification 0.1, which
postcard-schemaimplements
Work towards the Postcard Specification and portions of the Postcard 1.0 Release were sponsored by Mozilla Corporation.
All signed and unsigned integers larger than eight bits are encoded using a varint.
This includes the length of array slices, as well as the discriminant of enums.
For more information, see the varint requirements of the wire specification,
starting at postcard[atom.varint.encoding.continuation].
Postcard can serialize and deserialize messages similar to other serde formats.
Using the postcard-heapless crate, with its heapless-v0_9 feature, to serialize to a heapless::Vec<u8>:
use core::ops::Deref;
use serde::{Serialize, Deserialize};
use postcard::from_bytes;
use postcard_heapless::v0_9::{heapless::Vec, to_vec};
#[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, 11> = to_vec(&RefStruct {
bytes: &bytes,
str_s: message,
}).unwrap();
assert_eq!(
&[0x04, 0x01, 0x10, 0x02, 0x20, 0x05, b'h', b'E', b'l', b'L', b'o',],
output.deref()
);
let out: RefStruct = from_bytes(output.deref()).unwrap();
assert_eq!(
out,
RefStruct {
bytes: &bytes,
str_s: message,
}
);Or the optional alloc feature to serialize to an alloc::vec::Vec<u8>:
use core::ops::Deref;
use serde::{Serialize, Deserialize};
use postcard::{from_bytes, to_vec};
extern crate alloc;
use alloc::vec::Vec;
#[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> = to_vec(&RefStruct {
bytes: &bytes,
str_s: message,
}).unwrap();
assert_eq!(
&[0x04, 0x01, 0x10, 0x02, 0x20, 0x05, b'h', b'E', b'l', b'L', b'o',],
output.deref()
);
let out: RefStruct = from_bytes(output.deref()).unwrap();
assert_eq!(
out,
RefStruct {
bytes: &bytes,
str_s: message,
}
);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 or deserialization process, and can be combined to obtain complex protocol formats.
See the documentation of the ser_flavors or de_flavors modules for more information on usage.
Add postcard and the no-std subset of serde to the
[dependencies] section of your Cargo.toml.
Don't forget to disable default features on both, and enable only the features you use:
[dependencies]
postcard = { version = "2.0.0-rc.1", default-features = false }
# By default, `serde` has the `std` feature enabled, which makes it unsuitable for embedded targets
# disabling default-features fixes this. Enable `derive`, or use `serde_derive` directly.
serde = { version = "1.0", default-features = false, features = ["derive"] }Or, with cargo add:
cargo add postcard --no-default-features
cargo add serde --no-default-features --features deriveOther features, such as alloc, std, or defmt-v1, can be added to the features
list as needed. For heapless support, add the postcard-heapless crate with its
heapless-v0_9 feature.
For COBS framing (to_slice_cobs, from_bytes_cobs, CobsAccumulator), add the
postcard-cobs crate.
Library crates should always set default-features = false on postcard, as Cargo unifies
features across the dependency graph.
In the case serde attributes are used with postcard, the serialization and deserialization
process should be tested extensively as multiple attributes can cause problems.
See the tracking issue
The Serde Attributes section of the serde specification lists the attributes believed to cause issues with postcard, and those believed not to.
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
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.
