Skip to main content

Command line compiler for generating fastproto C++ headers

Project description

fastproto

fastproto is an early work-in-progress compiler for generating small C++ header files from .fastproto schema files. It is experimenting with a compact, header-only serialization API for C++ projects.

The generated header includes the fastproto runtime, so consumers only need to include the one generated file.

Quick Start

Generate a header from a schema:

uv run fastproto -o generated.hpp example.fastproto

Use it from C++:

#include "generated.hpp"

fastproto::Factory<fastproto::generated::my_namespace::SimpleMessage> factory;
factory.set_simple_int_field(10);

auto serialized = factory.serialize();

fastproto::Parser<fastproto::generated::my_namespace::SimpleMessage> parser;
parser.parse(serialized);
auto value = parser.get_simple_int_field();

Build the example:

c++ -std=c++23 example.cpp -o example

Schema Syntax

Schemas are organized into namespaces and messages:

namespace my_namespace {

message SimpleMessage(1) {
    simple_int_field : int32;
}

message MessageWithArray(3) {
    my_array_field : array<SimpleMessage>;
}

}

Each message has a numeric magic value, which is used when validating parsed buffers. Fields use name : type; syntax.

Currently supported field types include:

  • Integers: int8, int16, int32, int64, uint8, uint16, uint32, uint64
  • Floating point: float32, float64
  • bool
  • string
  • array<T> for arrays of simple types/messages
  • Other simple message types

Nested namespaces are supported with C++-style :: syntax:

namespace my_namespace::my_sub_namespace {
    message Nested(2) {
        value : int32;
    }
}

CLI

Run from the repo with uv:

uv run fastproto -o generated.hpp example.fastproto

Or install the command locally:

uv tool install --editable .

Then run:

fastproto -o generated.hpp example.fastproto

If -o is omitted, the generated header is written to stdout.

Python Bindings

fastproto can also generate a small Python package containing pybind11 bindings for a schema:

fastproto --python-out fastproto_example example.fastproto

This creates ./fastproto_example with the generated C++ header, pybind11 binding source, pyproject.toml, CMakeLists.txt, and Python package glue. The compiled extension module is named _fastproto_example and is installed inside the fastproto_example package. In this mode, --python-out is the output directory and package name, so -o is not used.

The generated directory looks like:

fastproto_example/
  pyproject.toml
  CMakeLists.txt
  include/generated.hpp
  src/fastproto_example_bindings.cpp
  fastproto_example/__init__.py

Build the package with:

uv build fastproto_example

Or install it into the current environment:

uv pip install ./fastproto_example

Fastproto namespaces become Python submodules. For the top-level example.fastproto, message factories and parsers are available under fastproto_example.my_namespace:

from fastproto_example.my_namespace import SimpleMessageFactory, SimpleMessageParser

factory = SimpleMessageFactory()
factory.simple_int_field = 10

data = factory.serialize()

parser = SimpleMessageParser(data)
print(parser.simple_int_field)

Nested fastproto namespaces map to nested Python modules:

from fastproto_example.my_namespace.my_sub_namespace.my_next_sub_namespace import (
    MessageWithStringFactory,
    MessageWithStringParser,
)

factory = MessageWithStringFactory()
factory.my_int_field = 42
factory.my_string_field = "Hello from Python"

parser = MessageWithStringParser(factory.serialize())
print(parser.my_int_field, parser.my_string_field)

The Python binding generator is also early WIP. It currently emits a pybind11 package scaffold that can be built with scikit-build-core, and it embeds the Python binding runtime into the generated binding source.

Design Notes

fastproto is currently optimized for low-copy serialization and parsing on common GCC/Clang targets. Generated messages use [[gnu::packed]] structs, and parsers view caller-owned byte buffers directly instead of materializing decoded objects.

That keeps the hot path small:

  • Simple messages serialize as a span over the factory-owned struct.
  • Strings parse as std::string_view into the input buffer.
  • Arrays parse as spans or indexed views into the input buffer.
  • Builtin scalar values are converted to/from network byte order.

The tradeoff is that this is not trying to be a maximally portable C++ wire format runtime. It assumes GCC/Clang-style packed-struct behavior on common architectures. The parser currently reinterprets bytes as generated structs, which is fast, but leans on compiler and platform behavior around packed layout, alignment, and object lifetime.

Status

This project is still early WIP. The schema language, generated C++ API, binary format, and runtime are all subject to change.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastproto_compiler-0.0.1a0.tar.gz (27.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastproto_compiler-0.0.1a0-py3-none-any.whl (18.4 kB view details)

Uploaded Python 3

File details

Details for the file fastproto_compiler-0.0.1a0.tar.gz.

File metadata

  • Download URL: fastproto_compiler-0.0.1a0.tar.gz
  • Upload date:
  • Size: 27.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fastproto_compiler-0.0.1a0.tar.gz
Algorithm Hash digest
SHA256 446772035979dc39e8f34e99c94103b1ebbf73e1bca7a654a9e2f65ba1faa4c2
MD5 750270d184eb59a0b147a65461bb6c02
BLAKE2b-256 bfb1fed336f621b63125b57765babcd2346ba048b57582eda415052559e8ca16

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastproto_compiler-0.0.1a0.tar.gz:

Publisher: publish.yml on humz2k/fastproto

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastproto_compiler-0.0.1a0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastproto_compiler-0.0.1a0-py3-none-any.whl
Algorithm Hash digest
SHA256 93ac754eaf09c433b74da6e04b0f804de308902bc5026f0407d79da18322cca8
MD5 a258a6bcc200b041b4d1e797235761cd
BLAKE2b-256 887dcc2bf80af926c1a734563be2bd6799fc6025095a436082c5b526046a88fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastproto_compiler-0.0.1a0-py3-none-any.whl:

Publisher: publish.yml on humz2k/fastproto

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page