OmniBuf
for Elixir.
Pure Elixir library on Hex.pm built for BEAM binary pattern matching and iodata. Define compile-time OmniBuf.Schema structs, bridge elixir-protobuf modules, or generate codecs and zero-copy sub-binary Opaque views.
def deps do
[
{:omnibuf, "~> 0.1.0"}
]
end$ OMNIBUF_BUILD_NATIVE=1 mix deps.compile omnibufThree ways to use OmniBuf in Elixir.
All three emit identical v1 DRAFT-1 binaries on Elixir 1.17+ and Erlang/OTP 27+.
Compile-time OmniBuf.Schema DSL
Declare numbered fields straight inside an Elixir module with use OmniBuf.Schema. Macros compile specialized iodata encoders and pattern-matching decoders directly into your module.
defmodule MyApp.Location do
use OmniBuf.Schema, name: "Location"
field 3, :city, :string
field 4, :rack, :uint32
field 5, :latitude, :float
field 6, :longitude, :float
end
wire = MyApp.Location.encode(%MyApp.Location{city: "Frankfurt", rack: 42})
{:ok, loc} = MyApp.Location.decode(wire)- Elixir type
- Protobuf type
- non_neg_integer() (:uint32 / :uint64)
- uint32 / uint64
- integer() (:sint32 / :sint64)
- sint32 / sint64
- boolean() (:bool)
- bool
- float() (:float / :double)
- float / double
- String.t() (:string)
- string
- binary() (:bytes)
- bytes
- atom() | integer() (:enum)
- enum
- struct() (Module)
- message
- [T] ({:repeated, T})
- repeated T
- %{K => V} ({:map, K, V})
- map<K, V>
Each field declaration in use OmniBuf.Schema picks its wire type explicitly (:uint32, :uint64, :sint32, :sint64, :fixed32, :sfixed32, :fixed64, :sfixed64, :float, :double, :string, :bytes). Fields set to nil are omitted on encode.
Existing elixir-protobuf structs
Services already generating use Protobuf structs pass them directly to OmniBuf.encode_proto/1 and OmniBuf.decode_proto/2.
wire = OmniBuf.encode_proto(pb_struct)
{:ok, decoded} = OmniBuf.decode_proto(wire, MyApp.PB.PeerUpdate)Generated modules and Opaque views
Build the escript plugin with mix escript.build and run buf generate. Generated <Message>.Opaque views slice untouched fields as zero-copy sub-binaries and splice single-field updates in ~2,420 ns.
version: v2
clean: true
plugins:
- local: ./protoc-gen-omnibuf-elixir
out: lib/generated$ mix escript.build
$ buf generatealias Omnibuf.Bench.{Location, PeerUpdate}
city =
packet
|> PeerUpdate.Opaque.new()
|> PeerUpdate.Opaque.get_location()
|> Location.Opaque.get_city()
updated =
packet
|> PeerUpdate.Opaque.new()
|> PeerUpdate.Opaque.set_sequence(9_876_543_211)
|> PeerUpdate.Opaque.marshal!()gRPC in Elixir with grpc-elixir.
OmniBuf.GRPC.Codec and OmniBuf.GRPC.SelfDescribingCodec implement the GRPC.Codec callbacks for grpc-elixir servers and stubs without adding :grpc as a runtime dependency.
Codec names: "omnibuf" (compact) · "x-omnibuf" (with Field #0)
# Server endpoint (zero runtime dependency on :grpc)
defmodule MyApp.Endpoint do
use GRPC.Endpoint
run MyApp.PingServer, codecs: [OmniBuf.GRPC.Codec, OmniBuf.GRPC.SelfDescribingCodec]
end
# Client call
{:ok, reply} = MyApp.PingStub.ping(channel, req, codec: OmniBuf.GRPC.Codec)Elixir questions.
Why is OmniBuf.Schema as fast as generated code on the BEAM?
use OmniBuf.Schema expands at compile time into specialized iodata encoders and binary pattern-matching decoders. There is no runtime schema interpretation, so the DSL matches generated plugin modules in both encode and decode throughput.
How do Opaque views avoid copying untouched fields in Elixir?
BEAM binaries support zero-copy sub-binary slicing. PeerUpdate.Opaque records byte spans for untouched fields and reassembles mutated packets as iodata, keeping unmodified nested payloads as references into the original binary.
How does OmniBuf.decode_blind inspect packets without a schema?
OmniBuf.decode_blind/1 and OmniBuf.decode_blind_json/1 walk the 4-bit wire types directly into an Elixir map or JSON string. Unnamed fields default to field_1, field_2, ... while packets encoded with self_describing: true resolve Field #0 names recursively.
wire = MyApp.Location.encode(loc, self_describing: true)
{:ok, map} = OmniBuf.decode_blind(wire)
{:ok, json} = OmniBuf.decode_blind_json(wire)