Skip to main content

Crate irontide_bencode

Crate irontide_bencode 

Source
Expand description

Serde-based bencode codec for BitTorrent.

Bencode is the serialization format used throughout BitTorrent for .torrent files, tracker responses, and DHT messages. It has four types:

  • Integers: i42e, i-1e, i0e
  • Byte strings: 4:spam (length-prefixed)
  • Lists: l<values>e
  • Dictionaries: d<key><value>...e (keys are byte strings, sorted)

§Usage

use serde::{Serialize, Deserialize};
use irontide_bencode::{to_bytes, from_bytes};

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Torrent {
    announce: String,
    #[serde(rename = "piece length")]
    piece_length: i64,
}

let torrent = Torrent {
    announce: "http://tracker.example.com/announce".into(),
    piece_length: 262144,
};

let encoded = to_bytes(&torrent).unwrap();
let decoded: Torrent = from_bytes(&encoded).unwrap();
assert_eq!(torrent, decoded);

Structs§

Deserializer
Bencode deserializer.
Serializer
Bencode serializer.

Enums§

BencodeValue
A dynamically-typed bencode value.
Error
Errors that can occur during bencode serialization or deserialization.

Functions§

find_dict_key_span
Find the raw byte span of a value for a given key in a bencoded dictionary.
from_bytes
Deserialize a value from bencode bytes.
from_bytes_lenient
Deserialize a value from bencode bytes, accepting unsorted dictionary keys.
to_bytes
Serialize a value to bencode bytes.

Type Aliases§

Result
Result type alias for bencode operations.