ref: b06b45b4700c60a4868bd17644a5c0ec861885aa zig-bare/README.md -rw-r--r-- 1.5 KiB
b06b45b4ugla Handle too-big HashMap capacity 2 months ago

#zig-bare builds.sr.ht status

BARE Message Encoding implementation in Zig.


Tested on Zig 0.7.1.

# Use -Drelease-{fast, safe, small} for release build.
zig build

# Run tests.
zig build test


Declare your types as usual, then use Writer to write them, and Reader to read them:

const Foo = union(enum) {
    x: u64,
    y: []const u8,
    z: void,

const Bar = struct {
    a: f32,
    b: u8,
    c: Foo,

const x = Bar{
    .a = 3.14,
    .b = 2,
    .c = Foo{ .y = "hello" },

var buf: [12]u8 = undefined;
var fbs = io.fixedBufferStream(&buf);

try Writer.init().write(x, fbs.writer());
try fbs.seekTo(0);
var r = Reader.init(allocator);
defer r.deinit();
const y = try r.read(Bar, fbs.reader());
warn("x: {}\ny: {}\n", .{ x, y });

Hash maps and slices require allocation; call deinit on a Reader to free any memory that was allocated.

#BARE conformance

All BARE types are supported, although data<length> and data are the same as [length]u8 and []u8, and string is also the same as []u8.

For map[type A]type B, std.HashMap is used.

When using Writer, all BARE invariants are enforced at compile-time.

#Code generation

There is no schema parser or code generation.

I don't need it myself, but I would gladly accept contributions.


Send patches and questions to ~alva/zig-bare@lists.sr.ht.