Function serde_bencode::ser::to_string

source ·
pub fn to_string<T: Serialize>(b: &T) -> Result<String>
Expand description

Serialize the given data into a String of bencode.

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(),
};

assert_eq!(
    serde_bencode::to_string(&address)?,
    "d4:city18:Duckburg, Calisota6:street17:1313 Webfoot Walke".to_string(),
);

Errors

Serialization can fail if T’s implementation of Serialize decides to fail or T contains floating point values, which bencode cannot serialize.