pub fn to_vec<T, const B: usize>(value: &T) -> Result<Vec<u8, B>>
Expand description
Serialize a T
to a heapless::Vec<u8>
, with the Vec
containing
data in a serialized format.
§Example
use postcard::to_vec;
use heapless::Vec;
use core::ops::Deref;
let ser: Vec<u8, 32> = to_vec(&true).unwrap();
assert_eq!(ser.deref(), &[0x01]);
let ser: Vec<u8, 32> = to_vec("Hi!").unwrap();
assert_eq!(ser.deref(), &[0x03, b'H', b'i', b'!']);
// NOTE: postcard handles `&[u8]` and `&[u8; N]` differently.
let data: &[u8] = &[0x01u8, 0x00, 0x20, 0x30];
let ser: Vec<u8, 32> = to_vec(data).unwrap();
assert_eq!(ser.deref(), &[0x04, 0x01, 0x00, 0x20, 0x30]);
let data: &[u8; 4] = &[0x01u8, 0x00, 0x20, 0x30];
let ser: Vec<u8, 32> = to_vec(data).unwrap();
assert_eq!(ser.deref(), &[0x01, 0x00, 0x20, 0x30]);