toby / serde-bencode

Serde backed Bencode encoding/decoding library for Rust.
MIT License
65 stars 16 forks source link

Encoding Vec<u8> as bytes #13

Closed katyo closed 6 years ago

katyo commented 6 years ago

I would like to use this library to handle BT DHT messages. I represent some fields (like TransactionId, NodeId) as Vec but I see that it encoded as arrays of integers, not as byte strings as expected. I don't sure that it is a wrong behavior, but I need a way to fix it.

katyo commented 6 years ago

Oh, I already found solution. Please, close this issue.

DCjanus commented 3 years ago

Hi, what solution you've found?

oreophilum commented 2 years ago

For future reference: You can annotate struct fields with #[serde(with = "serde_bytes")] from the serde_bytes-crate.

For example:


#[derive(Serialize, Deserialize)]
pub struct File {
    pub length: i64,
    #[serde(rename = "pieces root")]
    #[serde(with = "serde_bytes")]
    pub pieces_root: Vec<u8>,
}