I've done a little bit of research, and I've come to the conclusion that a lot of libraries that implement variable integers use deserialize_with, like, #[serde(deserialize_with="varint_decode")]. I'm interested in having a newtype, like, pub struct VarInt(i32); to represent them, instead of having to declare the field attribute each time, as they're common in the protocol I'm working with. I thought about implementing Deserialize for VarInt, but I'm sort of confused about it, since the Deserializer should do the actual decoding. Any advice?
#Serde varint type
20 messages · Page 1 of 1 (latest)
What is variable integer? You mean fixed precision numbers?
No, I mean unfixed numbers. Like zigzag
Does your protocol support both variable- and fixed-width encoding?
Yes, which does add to the issue. I would've just decoded all integers as varints otherwise :p
Right. You have to do horrible hacks in that case
Because you’re goïng outside the Serde data model
(this is why Serde is actually not very good at most things)
Basically, detect when the user calls deserialize_unit_struct("37ඞඞ__YOURFORMAT_HACK_VARINT_DO_NOT_USE__ඞඞ", v) in your Deserializer
and then if they call that, give them the varint
Ah, so, just deserialize different if it's a struct of the name VarInt or VarLong?
Actually, I suppose I could do deserialize_newtype_struct with the same technique?
It doesnt matter which method you hack into, yeah
It has to be a name the user wouldn’t possibly think of using
otherwise you get collisions
Hence why I suggest 37ඞඞ__YOURFORMAT_HACK_VARINT_DO_NOT_USE__ඞඞ
It starts with a number so can’t be a struct name, and has uncommon characters, etc
but it doesn’t matter it’ll just a const buried somewhere in your code
This is hurting me tbh.