OmniBuf
for Swift.
Zero-dependency SwiftPM core for macOS, iOS, and Linux. Map plain Swift structs with key paths, bridge existing SwiftProtobuf messages, or generate single-pass encoders and lazy Opaque views for zero-decode field reads and wire splicing.
dependencies: [
.package(url: "https://github.com/omnibuf/omnibuf-swift.git", from: "0.1.2"),
]Three ways to use OmniBuf in Swift.
Every path produces identical v1 DRAFT-1 wire bytes on Apple silicon and Linux.
Declarative Swift structs with key paths
Conform any Swift value type to OmniBufStruct and list its numbered fields with OmniBufStructSchema. Recursive messages work via Indirect<T>, with no .proto files or codegen step.
import OmniBuf
struct Location: OmniBufStruct, Equatable {
var city: String = ""
var rack: UInt32 = 0
static let omniBufSchema = OmniBufStructSchema<Self>(
typeName: "Location",
fields: [
Field.string(3, "city", \.city),
Field.uint32(4, "rack", \.rack),
]
)
}
let bytes = try Location(city: "FRA", rack: 42).encodeOmniBuf()
let decoded = try Location.decoded(from: bytes)- Swift type
- Protobuf type
- UInt32 / UInt64
- uint32 / uint64
- Int32 / Int64
- sint32 / sint64
- Bool
- bool
- Float / Double
- float / double
- String
- string
- Data, [UInt8]
- bytes
- RawRepresentable (Int32)
- enum
- OmniBufStruct, Indirect<T>
- message
- [T]
- repeated T
- [K: V]
- map<K, V>
Field.* constructors in OmniBufStructSchema bind Swift key paths directly to wire types (including Field.fixed32, Field.sfixed32, Field.fixed64, and Field.sfixed64). Optional fields (T?) omit nil on encode.
Existing SwiftProtobuf.Message types
The optional OmniBufProtobuf product implements custom SwiftProtobuf.Visitor and SwiftProtobuf.Decoder drivers. Pass any generated SwiftProtobuf.Message straight through OmniBufProtobuf.encode and decode.
import OmniBufProtobuf
let pkt = try OmniBufProtobuf.encode(protoPeer)
let copy: Omnibuf_Bench_PeerUpdate = try OmniBufProtobuf.decode(from: pkt)Generated structs and Opaque views
protoc-gen-omnibuf-swift emits Swift structs with single-pass backpatching encoders and lazy Opaque* views. Reading a nested string takes ~146 ns and splicing a single counter update takes ~513 ns.
version: v2
clean: true
plugins:
- local: protoc-gen-omnibuf-swift
out: Sources/Generated$ buf lint
$ buf generateimport OmniBuf
let pkt = try peer.encodeOmniBuf()
var view = OpaquePeerUpdate(pkt)
let city = view.getLocation().getCity()
view.setSequence(view.getSequence() + 1)
let updated = view.marshal()gRPC & Connect in Swift.
Two separate SwiftPM products (OmniBufGRPC and OmniBufConnect) keep core OmniBuf zero-dependency while providing Sendable serializers, deserializers, and Connect codecs.
Codec names: "omnibuf" (compact) · "x-omnibuf" (with Field #0)
// gRPC-Swift (separate SPM product: OmniBufGRPC)
import OmniBufGRPC
let serializer = OmniBufGRPCSerializer<PingRequest>()
let deserializer = OmniBufGRPCDeserializer<PingResponse>()
// Connect-Swift (separate SPM product: OmniBufConnect)
import OmniBufConnect
let codec = OmniBufConnectCodec.compactSwift questions.
Does the core OmniBuf module depend on SwiftProtobuf?
No. OmniBuf uses only the Swift standard library and ships the wire reader, single-pass backpatching writer, strict UTF-8 validator, OmniBufStruct schema codec, lazy Opaque scanners, and decodeBlind. Add OmniBufProtobuf only for existing SwiftProtobuf.Message types.
Where do prebuilt protoc-gen-omnibuf-swift binaries live?
Tagged releases on GitHub Releases attach darwin-arm64 and linux-amd64 archives with SHA-256 checksums. You can also build it from source with swift build -c release --product protoc-gen-omnibuf-swift.
How does decodeBlind inspect packets without a schema?
decodeBlind(pkt) and decodeBlindJSON(pkt) read 4-bit wire types directly into a BlindValue tree or JSON string. Fields fall back to field_1, field_2, ... unless Field #0 metadata was embedded via selfDescribing: true.
import OmniBuf
let pkt = try Location(city: "FRA", rack: 42).encodeOmniBuf(
options: EncodeOptions(selfDescribing: true)
)
let json = try decodeBlindJSON(pkt)