Function postcard::to_vec_crc32

source ·
pub fn to_vec_crc32<'a, T, const B: usize>(
    value: &T,
    digest: Digest<'a, u32>
) -> Result<Vec<u8, B>>where
    T: Serialize + ?Sized,
Available on crate features use-crc and heapless only.
Expand description

Conveniently serialize a T to a heapless::Vec<u8>, with the Vec containing data followed by a 32-bit CRC. The CRC bytes are included in the output Vec.

Example

use crc::{Crc, CRC_32_ISCSI};
use heapless::Vec;
use core::ops::Deref;

// NOTE: postcard handles `&[u8]` and `&[u8; N]` differently.
let data: &[u8] = &[0x01u8, 0x00, 0x20, 0x30];
let crc = Crc::<u32>::new(&CRC_32_ISCSI);
let ser: Vec<u8, 32> = postcard::to_vec_crc32(data, crc.digest()).unwrap();
assert_eq!(ser.deref(), &[0x04, 0x01, 0x00, 0x20, 0x30, 0x8E, 0xC8, 0x1A, 0x37]);

let data: &[u8; 4] = &[0x01u8, 0x00, 0x20, 0x30];
let ser: Vec<u8, 32> = postcard::to_vec_crc32(data, crc.digest()).unwrap();
assert_eq!(ser.deref(), &[0x01, 0x00, 0x20, 0x30, 0xCC, 0x4B, 0x4A, 0xDA]);

See the ser_flavors::crc module for the complete set of functions.