(serde) Deserialize Collection with capacity without caching

Hello,

I am working on a crate where I want to offer VecDeque-based collection types.

I want to enable the user to serialize the current capacity so the same capacity can be restored when deserializing.

My approach so far is to just to serialize the capacity as an extra field and on deserializing to first create the VecDeque and then to reallocate the deque to the desired capacity.

However, I wonder if I can create it without reallocating by using VecDeque::with_capacity in the deserialization.

The issue is, I want to leave it up to the user which data format the data is serialized/deserialized to. So I can't rely on my visitor to first find the capacity value and then the sequence. If it is the other way around I would have to cache again.

Then I had the idea to encode the capacity into the tag value of an enum that has basically usize::MAX identical variants. Because, serde must be able to first determine the tag before deserializing the variant-specific fields. Right?

I just cannot figure out how to be the tag ser/de integer based and not &str based as serde does under the hood.

I'd imagine finding a solution could work like that:

  1. Find a way to define the enum variant within the serde API by the repr value
  2. "Fake" usize::MAX identical variants in serialization and deserialization without actually defining that many variants
  3. Have the serde API tell me the capacity before giving me the sequence to deserialize it
#[repr(usize)]
enum WithCapacity<T> {
    //basically have usize::MAX identital variants with usize as the tag
    Deque(VecDeque<T>) = 100,
}

There is the serde_repr crate but it seems to be limited to field-less enums.

What could I do here?

I naively made this, is this actually working as it seems like?

This should only be an issue for map-like encodings; which includes serialize_struct, but not serialized_tuple or serialize_tuple_struct. Checking the Deserialize-impls for tuple types and tuple-struct types, the precedent from serde is clear that those should only require to support visit_seq, not visit_map; and the elements in a sequence are always in-order.

TL;DR, you can serialize your VecDeque<T>+capacity like (usize, VecDeque<T>), or like struct SomeStructName(usize, VecDeque<T>).

1 Like

Oh this is relieving, thank you.

This approach and implementation seems fine to me as well.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.