Function serde_bencode::de::from_bytes

source ·
pub fn from_bytes<'de, T>(b: &'de [u8]) -> Result<T>
where T: Deserialize<'de>,
Expand description

Deserialize an instance of type T from a bencode byte vector.

Examples

use serde_derive::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
struct Address {
    street: String,
    city: String,
}

let encoded = "d4:city18:Duckburg, Calisota6:street17:1313 Webfoot Walke".as_bytes();
let decoded: Address = serde_bencode::from_bytes(&encoded)?;

assert_eq!(
    decoded,
    Address {
        street: "1313 Webfoot Walk".to_string(),
        city: "Duckburg, Calisota".to_string(),
    }
);

Errors

This conversion can fail if the input bencode is improperly formatted or if the structure of the input does not match the structure expected by T. It can also fail if T’s implementation of Deserialize decides to fail.