Open void-dragon opened 2 years ago
serde_bytes
only supports a few types and cannot support arbitrary data structures. You can use the serde_with::Bytes
type, which is more powerful than what serde_bytes
offers and can be used with more collections. The crate documentation shows how you can use it in nested situations.
#[serde_with::serde_as]
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag="kind")]
pub enum Envelope<T> {
AllKnown {
#[serde_as(as = "Vec<serde_with::Bytes>")]
all_known: Vec<PubKey>
},
}
If you want to use serde_bytes
, you will either need to write the deserialization logic for Vec<PubKey>
yourself, for example by deserializing into a Vec<ByteBuf>
and turning that into a Vec<PubKey>
, or aliasing PubKey
to ByteBuf
, which already has the serialization behavior you want.
Something like this should work:
pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<PubKey>, D::Error>
where
D: Deserializer,
{
let tmp: Vec<serde_bytes::ByteBuf> = Vec::deserialize(deserializer)?;
Ok(tmp.into_iter().map(|x| x.into_vec()).collect())
}
Hi, i have a message to send in the form of:
As you can see,
PubKey
is only aVec<u8>
, what is the most idiomatic way to pack this. I do not want to put theVec<u8>
in a struct to useserde_bytes
. Is there something i am missing? It would be really neat to annotatedall_known
just with#[serde(with="serde_bytes")]
. It seems a pretty common problem to be to have an array of byte arrays.