I have a struct that I want to use. I want to be able to Encode/Decode it using bincode. How can I add a custom function to do that?
I know for Serialize/Deserialize I can add an attribute #[serde(with = "my_serde")] and define a module with the function serialize and deserialize . I found a great source here Custom serialization · Serde .
However, for bincode Encode/Decode I can't find what to do.
I'm sorry, I'm new to rust. So I'm still learning what the terms "subset" and "compatibility layer" are. My struct that I want to add support to is not owned by me and does not support serde. So I added an attribute to support it myself
#[serde(with = "ciphertext_serde")]
cipher_text: CipherText
mod ciphertext_serde { ... }
Now I can derive Serialize/Deserialize from my struct that holds it. However, I cannot simply write
If I try to add something like #[bincode(with = "ciphertext_bincode")]
I get the error (pointing to with) saying Unknown field attribute. What does that mean?
presumably I could. I will if I have to. However, what is the reason that both #[bincode(with = "ciphertext_bincode")] and #[bincode(with_serde)] don't work?
serde_with = "..." doesn't implement Serialize or Deserialize for the type of the field, but rather is an ad-hoc way to insert alternative (de)serialisation logic into serde's processing. This is incompatible with the code generated by the bincode::Encode/bincode::Decode macros, which relies on Serialize/Deserialize actually being implemented for the type.