Function postcard::to_vec [−][src]
pub fn to_vec<B, T: ?Sized>(value: &T) -> Result<Vec<u8, B>> where
T: Serialize,
B: ArrayLength<u8>,
Serialize a T
to a heapless::Vec<u8>
, with the Vec
containing
data in a serialized format. Requires the (default) heapless
feature.
Example
use postcard::to_vec; use heapless::{Vec, consts::*}; use core::ops::Deref; let ser: Vec<u8, U32> = to_vec(&true).unwrap(); assert_eq!(ser.deref(), &[0x01]); let ser: Vec<u8, U32> = 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, U32> = 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, U32> = to_vec(data).unwrap(); assert_eq!(ser.deref(), &[0x01, 0x00, 0x20, 0x30]);