Serializing a static string reference with Serde and Bincode


#1

My intention is to serialize various structures into a byte vector (Vec < u8 >) tape-style.
These structures are limited to 'static references or contain no references at all.
The only operations that can happen on the tape is read/write/seek forward/seek backward, like on a single-tape turing state machine.

There are a few requirements

  • I need the serialized bytes to only produce a valid deserialization during the execution of the program that created the byte representation
  • The tape is used as a stack so the last serialized structure is always deserialized (popped) first
  • The structures must implement Copy

At first i was looking into ‘just dumping’ the bytes onto the vector, but decided before investigating that option that using Serde might be a safer approach to solve this. This is also an excuse to use Serde for the first time.

I tried to build something with bincode::serialize_into() and bincode::deserialize_from() and noticed the serialization process also writes string contents (when a &'static str is passed) into the bytes, which is non desired serialization behaviour.
The second issue is that i haven’t quite figured out how to properly tell the compiler that any references deserialized (should) have a static lifetime.

Deserializing is also messy because the read-head is always located behind the last serialized structure. I solved this by always writing the serialized size (#bytes) after each data write. This reverse vector reading eventually feels non-idiomatic, i wish bincode devs accepted iterators instead of slices because of reverse iterators.

Am i approaching this wrong? I’d love some feedback.