I'm writing a Deserializer
for a format that already exists, but the format has one peculiarity which I don't know how to implement in serde:
// pseudo code
fn example(){
// when decoding an u8 directly, the value 0x00 is invalid
assert!(<u8>::deserialize(Format::from_bytes(&[0x00u8])).is_err());
// however, when decoding an u8 through a Vec<u8> 0x00 is valid
//
// The problem is that this will first call `deserialize_seq`, which
// will then use the same function as `<u8>::deserialize`, but I need
// it to call a different method which actually accepts the value 0x00
assert_eq!(<Vec<u8>>::deserialize(Format::from_bytes(&[0x00u8])), vec![0x00]);
}
The problem is that the original implementation is not generic, and it has an implementation for u8
which rejects the value 0x00
, and a Vec<u8>
which accepts it.
It looks like I need some kind of runtime type information to replicate this within a serde deserializer, any way of implementing this?