serde_bencode 0.2.0

A Serde backed Bencode encoding/decoding library for Rust.


A Serde backed Bencode encoding/decoding library for Rust.


Add the following to your Cargo.toml:

serde_bencode = "^0.1.0"
serde = "^1.0.0"
serde_derive = "^1.0.0"

Serde works best with Rust nightly, it is highly recommended that you use it.


This is an abbreviated .torrent parsing example from examples/ If you compile this crate as a binary, it will print metadata for any Torrent sent to stdin.

Bencode Enum

There is a Bencode enum provided when any valid Bencode value is needed in a single typed container. For example you can use it to serialize/deserialize type Vec<Bencode>:

let list: Vec<Bencode> = vec!["one".into(), "two".into(), "three".into(), 4i64.into()];
let mut ser = Encoder::new();
list.serialize(&mut ser).unwrap();
let list_serialize: Vec<u8> = ser.into();
assert_eq!(String::from_utf8(list_serialize).unwrap(), "l3:one3:two5:threei4ee");


In the example you'll notice that the is a serde::bytes::ByteBuf. This is a wrapper type provided by Serde that allows Vec<u8> to be decoded as a Bencode ByteString instead of a List.