#bittorrent #bencode

bin+lib benc0de

A super-faster bencode parser

2 unstable releases

0.2.0 Oct 23, 2020
0.1.0 Oct 23, 2020

#19 in #bencode

MIT license

15KB
340 lines

bencode

Super-fast, zero-allocation bencode parser written in Rust.

How fast is it?

Very fast! You can expect to parse AND extract data at a rate of 0.5 GB/s.

Quick start

let bs = "d4:listli1ei2ee4:ping4:ponge".as_bytes(); // Bencode data to parse.

let parser = BenParser::new();

let mut root = parser.parse(bs);

// Iterate through list of numbers
for item in root.get("list").iter_numbers() {
  // ...
}

// Get byte value from dict
let val = root.get("ping").bytes().unwrap();

// The parser is fault tolerant.
let val = root.get("ping").get("some_other").bytes(); // --> Returns None.

Performance tip

This library is especially fast, if you query dict entries alphabetically.

root.get("a").bytes().unwrap(); // Query bytes
root.get("b").number().unwrap(); // Query number (isize)
root.get("c").get("nested_entry").iter_bytes();

// Caching is also useful.
let c = root.get("c");

c.get("nested_entry1");
c.get("nested_entry2");
// ...

Comments

  • This is my first Rust project, so feel free to comment on this.

No runtime deps