Expand description
Yet another encoding/decoding library for bencode.
I believe deserializer don’t allocate memory. Serializer allocates
only when sort_dictionary feature enabled. I think this crate is fast enough.
§Examples
For .torrent parsing example see examples directory
§Caveats
serde treats [u8; N], Vec<u8>, &[u8] like any other
sequence, that is it will be encoded as list of bytes(not a byte string).
Solution - use serde_bytes.
§Serializing map with Option values
You will need to write a custom ser/de helper. For inspiration see https://github.com/serde-rs/serde/issues/550#issuecomment-246746639
§Serializing Option
Use #[serde(skip_serializing_if = "Option::is_none")] annotation or see
serde_with::skip_serializing_none
§Behaviour
This crate does sort dictionary by keys if sort_dictionary feature is
enabled (enabled by default). Otherwise order of elements can vary
(depends on Serialize trait implementation).
§Mapping of rust types to bencode
boolisintegereitheri1eori0e.all primitive integer typesisinteger.charisbyte stringwith length at most 4 bytes.Stringisbyte string.[u8]islistseeCaveats.Option, Serializing is not allowed but can be achieved. SeeSerializing Option.()(unit) is0:, emptybyte string.struct UnitStruct;is10:UnitStruct,byte stringcontaining name of the unit struct.enum E { A, B }E::Aisd1:E1:Ae,dictionarywith one entry. Key is name of theenumand value is the variant.
tuples andarrays islists, tuples can be heterogeneous.struct Rgb(u8, u8, u8)(tuple struct) islistof tuple values.HashMapisdictionary.structs isdictionary. Keys are field names, values are field values.f32,f64is not supported.
§Alternatives
Arbitrary order. Search more on crates.io or
lib.rs.
§Bencoding
§Supported data types
§Byte Strings
<base ten ASCII>:<string bytes>
Examples:
4:abcd0:
§Integers
i<base ten ASCII, optional minus sign>e
The maximum number is not specified. This crate
handles integers as u64 or i64.
Examples:
i123456ei-5e
Malformed:
i03e, anything that starts with 0, excepti0ei-0e
§Lists
l<bencode type values>e
Examples:
li0ei1ei2ee==[1,2,3]le==[]
§Dictionaries
d<bencoded string><bencoded element>e
Keys must be sorted as raw strings. string’s should be
compared using a binary comparison.
Examples:
de=={}d4:rustl2:is7:awesomeee=={"rust" => ["is", "awesome"]}
§Crate features
§sort_dictionary
Enables sort by keys when serializing to bencode dictionary.
Structs§
Enums§
Functions§
- from_
bytes - Deserializes bencoded bytes to rust’s value.
- from_
bytes_ auto - The same as
from_bytesbutdeserialize_anywill deserialize byte string asstrif input bytes are valid UTF-8, otherwise asbytes. - from_
str - Deserializes bencoded
&strto rust’s value. - from_
str_ auto - The same as
from_strbutdeserialize_anywill deserialize byte string asstrif input bytes are valid UTF-8, otherwise asbytes - to_
string - Serializes rust’s type to bencode string
- to_vec
- Convenient function to get encoded value as bytes
- to_
writer - Serializes rust’s type to bencode using
Writetrait