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:
- Find a way to define the enum variant within the serde API by the repr value
- "Fake"
usize::MAX
identical variants in serialization and deserialization without actually defining that many variants - 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?