[][src]Function postcard::to_vec

pub fn to_vec<B, T: ?Sized>(value: &T) -> Result<Vec<u8, B>> where
    T: Serialize,
    B: ArrayLength<u8>, 

Serialize a data structure to a heapless::Vec. The Vec must contain enough space to hold the entire serialized message, or an error will be returned.

Example

use postcard::to_vec;
use heapless::{Vec, consts::*};
use core::ops::Deref;

let input: &str = "hello, postcard!";
let output: Vec<u8, U17> = to_vec(input).unwrap();

// Length is serialized as a [`postcard::VarintUsize`]
assert_eq!(0x10, output.deref()[0]);

// otherwise, bytes/UTF-8 is serialized as-is
assert_eq!(input.as_bytes(), &output.deref()[1..]);