Function postcard::to_vec_cobs

source ·
pub fn to_vec_cobs<T, const B: usize>(value: &T) -> Result<Vec<u8, B>>
where T: Serialize + ?Sized,
Expand description

Serialize a T to a heapless::Vec<u8>, with the Vec containing data in a serialized then COBS encoded format. The terminating sentinel 0x00 byte is included in the output Vec.

§Example

use postcard::to_vec_cobs;
use heapless::Vec;
use core::ops::Deref;

let ser: Vec<u8, 32> = to_vec_cobs(&false).unwrap();
assert_eq!(ser.deref(), &[0x01, 0x01, 0x00]);

let ser: Vec<u8, 32> = to_vec_cobs("Hi!").unwrap();
assert_eq!(ser.deref(), &[0x05, 0x03, b'H', b'i', b'!', 0x00]);

// NOTE: postcard handles `&[u8]` and `&[u8; N]` differently.
let data: &[u8] = &[0x01u8, 0x00, 0x20, 0x30];
let ser: Vec<u8, 32> = to_vec_cobs(data).unwrap();
assert_eq!(ser.deref(), &[0x03, 0x04, 0x01, 0x03, 0x20, 0x30, 0x00]);

let data: &[u8; 4] = &[0x01u8, 0x00, 0x20, 0x30];
let ser: Vec<u8, 32> = to_vec_cobs(data).unwrap();
assert_eq!(ser.deref(), &[0x02, 0x01, 0x03, 0x20, 0x30, 0x00]);