Serialization with postcard and serde

I am trying to serialize a struct which stores some data as a Vec<u8>. I am serializing it with postcard and serde.

struct Test {
  buf: Vec<u8>,
}
use postcard;
use heapless::Vec;

pub encode() {
  let mut bytes = Vec<u8, 1000000>::new();
  for i in 0..1000000 {
     bytes.push(9);
  }
  let data = Test { buf: data.to_vec() };
  postcard::to_vec::<Test, 1000020>(data).unwrap();
}

I am trying to store about 1MB of data Test.buf, and I am getting fatal runtime error: stack overflow. Since postcard works with heapless::Vec is there a way around it?

I'm not an expert with postcard, but there is a alloc feature. Maybe this helps?

Since you don't have a heap, you can't process anything larger than your stack size. When copying, less than half the size of your stack.

You will need to perform streaming serialization that writes data immediately to some external storage.

1 Like

Since I am not moving data between embedded systems and can afford to use heap memory. I did cargo add postcard --features use-std. And used postcard::to_stdvec for serialization.