Skip to content

About

a protobuf implementation for zig.

Topics

Resources

Contributing

Stars

454 stars

Watchers

7 watching

Forks

Repository files navigation

zig-protobuf

State of the implementation

This repository implements protocol buffers for proto2, proto3 and editions (up to edition 2026), including extensions.

This project is mature enough to be used in production. It passes the upstream conformance suite (protobuf v36.2) for the proto2, proto3 and editions test messages, in binary and JSON. The text format is not supported.

The sections below are enough to start. doc/legacy.md has the rest, such as default values, extensions, options, streaming decode and the update from version 5.

Most of the code below comes from doc/example, a small project. To build and run it, run zig build run in doc/example.

How to use

Which branch ?

The master branch always targets the Latest Stable Version of zig.

If you want a version targeting the Bleeding Edge Development Version (zig's master branch), use the zig-master branch. It is updated regularly to be compatible, and merge semi-regularly master's development.

Each branch has its own README, with the instructions for that branch.

Add the library to your project

Add protobuf to your build.zig.zon:

zig fetch --save "git+https://github.com/Arwalk/zig-protobuf#master"

Then use the protobuf module. In your build.zig's build function, add the dependency as module before b.installArtifact(exe):

// first create a build for the dependency
const protobuf_dep = b.dependency("protobuf", .{
    .target = target,
    .optimize = optimize,
});

// and lastly use the dependency as a module
exe.root_module.addImport("protobuf", protobuf_dep.module("protobuf"));

Decode and encode a message

This program decodes a message, prints it, encodes it back and prints the bytes. It imports the code generated from a .proto file (refer to the next section):

const std = @import("std");
const example = @import("proto/example.pb.zig");

// A `Project` message, in hexadecimal.
const encoded_project = "0a180a0e70726f6a6563745f617574686f721206417277616c6b" ++
    "0a1c0a0c70726f6a6563745f6e616d65120c7a69672d70726f746f627566" ++
    "0a170a1070726f6a6563745f6c616e677561676512035a6967";

pub fn main(init: std.process.Init) !void {
    const allocator = init.gpa;

    var stdout_buffer: [1024]u8 = undefined;
    var stdout_writer = std.Io.File.stdout().writer(init.io, &stdout_buffer);
    const stdout = &stdout_writer.interface;

    var input_buffer: [encoded_project.len / 2]u8 = undefined;
    const input = try std.fmt.hexToBytes(&input_buffer, encoded_project);

    // Decode the message. This reader is for bytes, any other `std.Io.Reader` works too.
    var reader: std.Io.Reader = .fixed(input);
    var project: example.Project = try .decode(&reader, allocator);
    defer project.deinit(allocator);

    for (project.data.items) |entry| {
        try stdout.print("{s}: {s}\n", .{ entry.key, entry.value });
    }

    // Encode it back. This writer is for memory, any other `std.Io.Writer` works too.
    var output: std.Io.Writer.Allocating = .init(allocator);
    defer output.deinit();
    try project.encode(&output.writer, allocator);

    try stdout.print("{x}\n", .{output.written()});

    try stdout.flush();
}

It prints:

project_author: Arwalk
project_name: zig-protobuf
project_language: Zig
0a180a0e70726f6a6563745f617574686f721206417277616c6b0a1c0a0c70726f6a6563745f6e616d65120c7a69672d70726f746f6275660a170a1070726f6a6563745f6c616e677561676512035a6967

Generating .zig files out of .proto definitions

You can do this programmatically as a compilation step for your application. Take this definition, in proto/project.proto:

syntax = "proto3";

package example;

message Project {
  map<string, string> data = 1;
}

The following snippets of build.zig create a zig build gen-proto command for your project:

const protobuf = @import("protobuf");
const gen_proto = b.step("gen-proto", "generates zig files from protocol buffer definitions");

// The generator runs during the build: it is built for the host, not for `target`.
const protoc_step = protobuf.RunProtocStep.create(protobuf_dep.builder, b.graph.host, .{
    // out directory for the generated zig files
    .destination_directory = b.path("src/proto"),
    .source_files = &.{
        b.path("proto/project.proto"),
    },
    .include_directories = &.{},
    // Optional LazyPath to `protoc`. If null, zig-protobuf downloads Google's release of
    // the compiler. See "Building protoc from source" to avoid this binary.
    .protoc = null,
    // Preserve unknown fields during binary decode/encode round trips.
    // Defaults to false.
    .preserve_unknown_fields = false,
});

gen_proto.dependOn(protoc_step.step);

The path of the generated file is the package: src/proto/example.pb.zig here, and src/proto/a/b.pb.zig for a package a.b. Each message is a struct, with functions such as decode, encode and deinit. A map is a list of entries:

pub const Project = struct {
    data: std.ArrayList(Project.DataEntry) = .empty,

    pub const _desc_table = .{
        .data = fd(1, .{ .repeated = .submessage }),
    };

    pub const DataEntry = struct {
        key: []const u8 = &.{},
        value: []const u8 = &.{},

        pub const _desc_table = .{
            .key = fdf(1, .{ .scalar = .string }, .{ .utf8_validation = .verify }),
            .value = fdf(2, .{ .scalar = .string }, .{ .utf8_validation = .verify }),
        };

Building protoc from source

If you do not want a binary downloaded into your build, protocFromSource builds protoc from its sources, with nothing but the Zig toolchain. Use it in the options of RunProtocStep:

.protoc = protobuf.protocFromSource(protobuf_dep.builder),

This is the same version of protoc as the download, and it finds the well-known types such as google/protobuf/timestamp.proto on its own. It is always built for the machine that runs the build, whatever your target is.

The first build costs a download of about 12 MB of sources, close to 3 minutes of compilation on 16 cores, and about 1 GB in .zig-cache. Later builds reuse the cache.

The sources are only fetched by a call to protocFromSource, whatever step you run, and the Google release is then not downloaded. To make it a choice in your project, only call it when needed, for example behind a build option.

Service Code Generation

zig-protobuf generates code for Protocol Buffer service definitions as a VTable: a struct of function pointers, for the user data type and the error set that you give. This provides a flexible, type-safe interface for implementing gRPC-compatible services with custom server contexts.

Note: This generates service interfaces only. It does not include a gRPC transport layer - users must implement their own server logic and transport.

For detailed documentation on service code generation, including examples and usage patterns, see docs/services.md.


The zig-protobuf logo is licensed under the Attribution 4.0 International (CC BY 4.0).

The logo is inspired by the official mascots of the Zig programming language, themselves licensed under the Attribution 4.0 International (CC BY 4.0)

Original art by vivisector.


If you're really bored, you can buy me a coffee here.

ko-fi

About

a protobuf implementation for zig.

Topics

Resources

Contributing

Stars

454 stars

Watchers

7 watching

Forks

Releases

Packages

Used by

Contributors

Languages