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

anybuf

A minimal, zero dependency proto3 encoder to encode anything

5 releases (3 breaking)

0.4.0 Feb 10, 2024
0.3.0 Nov 1, 2023
0.3.0-rc.1 Oct 29, 2023
0.2.0 Oct 24, 2023
0.1.0 Apr 28, 2023

#791 in Encoding

Download history 132/week @ 2023-12-18 4/week @ 2023-12-25 86/week @ 2024-01-01 177/week @ 2024-01-08 139/week @ 2024-01-15 275/week @ 2024-01-22 191/week @ 2024-01-29 107/week @ 2024-02-05 148/week @ 2024-02-12 175/week @ 2024-02-19 228/week @ 2024-02-26 200/week @ 2024-03-04 265/week @ 2024-03-11 180/week @ 2024-03-18 163/week @ 2024-03-25 211/week @ 2024-04-01

827 downloads per month
Used in white-whale-std

Apache-2.0

110KB
1.5K 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 runtime deps