#codec #tor #data-encoding #arti


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

26 releases (12 breaking)

0.18.0 Apr 30, 2024
0.10.0 Feb 5, 2024
0.8.0 Dec 4, 2023
0.7.5 Oct 2, 2023
0.0.0 Jun 24, 2021

#191 in Encoding

Download history 171/week @ 2024-01-22 145/week @ 2024-01-29 364/week @ 2024-02-05 210/week @ 2024-02-12 143/week @ 2024-02-19 519/week @ 2024-02-26 1890/week @ 2024-03-04 615/week @ 2024-03-11 1011/week @ 2024-03-18 1606/week @ 2024-03-25 1122/week @ 2024-04-01 1457/week @ 2024-04-08 316/week @ 2024-04-15 281/week @ 2024-04-22 624/week @ 2024-04-29 456/week @ 2024-05-06

1,719 downloads per month
Used in 33 crates (10 directly)




Utilities to decode/encode things into bytes.


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.


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


~277K SLoC