Module postcard::ser_flavors
source · [−]Expand description
Flavors - Plugins for postcard
“Flavors” in postcard
are used as modifiers to the serialization
process. Flavors typically modify one or both of the following:
- The output medium of the serialization, e.g. whether the data is serialized to a
[u8]
slice, or aheapless::Vec
. - 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 SerFlavor
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!
Usability
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.
Examples
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::{
serialize_with_flavor,
ser_flavors::Slice,
};
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]>(
data,
Slice::new(buffer)
).unwrap();
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::{
serialize_with_flavor,
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]>(
data,
Cobs::try_new(Slice::new(buffer)).unwrap(),
).unwrap();
assert_eq!(res, &[0x03, 0x04, 0x01, 0x03, 0x20, 0x30, 0x00]);
Structs
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
.
TODO
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 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.
Traits
TODO
Type Definitions
TODO