Function serde_bencode::ser::to_bytes
source · pub fn to_bytes<T: Serialize>(b: &T) -> Result<Vec<u8>>
Expand description
Serialize the given data into a bencode byte vector.
Examples
use serde_derive::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
struct Address {
street: String,
city: String,
}
let address = Address {
street: "1313 Webfoot Walk".to_string(),
city: "Duckburg, Calisota".to_string(),
};
let bytes = serde_bencode::to_bytes(&address)?;
assert_eq!(
String::from_utf8(bytes).unwrap(),
"d4:city18:Duckburg, Calisota6:street17:1313 Webfoot Walke",
);
Errors
Serialization can fail if T
’s implementation of Serialize
decides to fail or T
contains
floating point values, which bencode cannot serialize.