#codec #protobuf #proto3 #proto #low-level #protobuf3

no-std anybuf

A minimal, zero dependency proto3 encoder to encode/decode anything. No schemas needed.

8 releases (4 breaking)

0.5.2 Dec 6, 2024
0.5.1 Dec 5, 2024
0.5.0 Jun 4, 2024
0.4.0 Feb 10, 2024
0.1.0 Apr 28, 2023

#166 in Encoding

Download history 234/week @ 2024-08-23 273/week @ 2024-08-30 260/week @ 2024-09-06 180/week @ 2024-09-13 194/week @ 2024-09-20 373/week @ 2024-09-27 656/week @ 2024-10-04 1304/week @ 2024-10-11 923/week @ 2024-10-18 891/week @ 2024-10-25 539/week @ 2024-11-01 447/week @ 2024-11-08 824/week @ 2024-11-15 902/week @ 2024-11-22 509/week @ 2024-11-29 664/week @ 2024-12-06

2,953 downloads per month
Used in 12 crates (3 directly)

Apache-2.0

120KB
2K SLoC

anybuf

anybuf on crates.io Docs

A minimal, zero dependency protobuf encoder and decoder to encode/decode anything. It is designed to create the value bytes of a protobuf Any, hence the name.

Due to its low level design, anybuf allows you to do things wrong in many ways and you should have a solid understanding of how protobuf encoding works in general to better understand the API.

The crate anybuf is split in two major components:

  • anybuf::Anybuf is a protobuf encoder
  • anybuf::Bufany is a protobuf decoder

Non goals

Supported

  • Varint fields (bool/uint32/uint64/sint32/sint64/int32/int64)
  • Variable length fields (string/bytes)
  • Nested messages: Just append an Anybuf instance
  • Repeated (bool/uint32/uint64/sint32/sint64/int32/int64/string/bytes/messages)

Not yet supported

How to use it

Encoding

use anybuf::Anybuf;

let data = Anybuf::new()
    .append_uint64(1, 4)        // field number 1 gets value 4
    .append_string(2, "hello")  // field number 2 gets a string
    .append_bytes(3, b"hello")  // field number 3 gets bytes
    .append_message(4, &Anybuf::new().append_bool(3, true)) // field 4 gets a message
    .append_repeated_uint64(5, &[23, 56, 192])              // field number 5 is a repeated uint64
    .into_vec();                // done

// data is now a serialized protobuf document

Decoding

use anybuf::Bufany;

let deserialized = Bufany::deserialize(&data).unwrap(); // data from above
let id = deserialized.uint64(1).unwrap(); // 4
let title = deserialized.string(2).unwrap(); // "hello"

no_std support

Since version 0.5.0 there is a default std feature. If you remove that, the library is built with no_std support. As the anybuf maintainers do not require no_std support this is provided at a best effort basis and might be broken.

[dependencies]
anybuf = { version = "0.5.0", default-features = false }

No runtime deps