#codec #tor #data-encoding #arti #format #helper

tor-bytes

Helpers for encoding and decoding byte-orientted data, as used by Tor

32 releases (18 breaking)

0.24.0 Oct 31, 2024
0.22.0 Sep 3, 2024
0.20.0 Jun 27, 2024
0.10.0 Feb 5, 2024
0.0.0 Jun 24, 2021

#224 in Encoding

Download history 1437/week @ 2024-07-28 1477/week @ 2024-08-04 1086/week @ 2024-08-11 1539/week @ 2024-08-18 1425/week @ 2024-08-25 1855/week @ 2024-09-01 1380/week @ 2024-09-08 1255/week @ 2024-09-15 1685/week @ 2024-09-22 1487/week @ 2024-09-29 967/week @ 2024-10-06 931/week @ 2024-10-13 1280/week @ 2024-10-20 1393/week @ 2024-10-27 2020/week @ 2024-11-03 1466/week @ 2024-11-10

6,217 downloads per month
Used in 54 crates (11 directly)

MIT/Apache

270KB
4K SLoC

tor-bytes

Utilities to decode/encode things into bytes.

Overview

The tor-bytes crate is part of Arti, a project to implement Tor in Rust. Other crates in Arti use it to build and handle all the byte-encoded objects from the Tor protocol. For textual directory items, see the tor-netdoc crate.

This crate is generally useful for encoding and decoding byte-oriented formats that are not regular enough to qualify for serde, and not complex enough to need a full meta-language. It is probably not suitable for handling anything bigger than a few kilobytes in size.

Alternatives

The Reader/Writer traits in std::io are more appropriate for operations that can fail because of some IO problem. This crate can't handle that: it is for handling things that are already in memory.

TODO: Look into using the "bytes" crate more here.

TODO: The "untrusted" crate has similar goals to our Reader, but takes more steps to make sure it can never panic. Perhaps we should see if we can learn any tricks from it.

TODO: Do we really want to keep Reader as a struct and Writer as a trait?

Contents and concepts

This crate is structured around four key types:

  • Reader: A view of a byte slice, from which data can be decoded.
  • Writer: Trait to represent a growable buffer of bytes. (Vec<u8> and bytes::BytesMut implement this.)
  • Writeable: Trait for an object that can be encoded onto a Writer
  • Readable: Trait for an object that can be decoded from a Reader.

Every object you want to encode or decode should implement Writeable or Readable respectively.

Once you implement these traits, you can use Reader and Writer to handle your type, and other types that are built around it.

License: MIT OR Apache-2.0

Dependencies

~10–21MB
~294K SLoC