~alva/zig-bare

e01f256bedbc17f519244f476552f4fef3a4766d — Alva 4 months ago 661dd70
Update README and main.zig example
2 files changed, 63 insertions(+), 29 deletions(-)

M README.md
M src/main.zig
M README.md => README.md +35 -13
@@ 16,26 16,42 @@ zig build test

## Usage

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

```zig
const Foo = struct {
const Foo = union(enum) {
    x: u64,
    y: []const u8,
    z: void,
};

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

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

const file = try fs.cwd().createFile("bare-test", .{ .read = true });
defer file.close();
const foo1 = Foo{ .a = 3.14, .b = 2, .c = true };
try Writer.init().write(foo1, file.outStream());
try file.seekTo(0);
var reader = Reader.init(allocator);
defer reader.deinit();
const foo2 = try reader.read(Foo, file.inStream());
assert(std.meta.eql(foo1, foo2));
var buf: [@sizeOf(Bar)]u8 = undefined;
var fbs = io.fixedBufferStream(&buf);

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

Hashmaps and slices require allocation;
You can call `deinit` on a `Reader` to free all memory that was allocated.
Hash maps and slices require allocation;
call `deinit` on a `Reader` to free any memory that was allocated,
if hash maps or slices are used.

## BARE conformance



@@ 46,6 62,12 @@ For `map[type A]type B`, `std.HashMap` is used.

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.

## Contributing

Send patches and questions to

M src/main.zig => src/main.zig +28 -16
@@ 7,24 7,36 @@ const bare = @import("bare.zig");
const Reader = bare.Reader;
const Writer = bare.Writer;

const Foo = struct {
    a: f32,
    b: u8,
    c: bool,
};

pub fn main() !void {
    var mem_buf: [0x10000]u8 = undefined;
    var fixed_alloc = std.heap.FixedBufferAllocator.init(&mem_buf);
    const allocator = &fixed_alloc.allocator;
    var mem: [0x10000]u8 = undefined;
    var fba = std.heap.FixedBufferAllocator.init(&mem);
    const allocator = &fba.allocator;

    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 = .{ .y = "hello" },
    };

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

    const file = try fs.cwd().createFile("bare-test", .{ .read = true });
    defer file.close();
    const foo1 = Foo{ .a = 3.14, .b = 2, .c = true };
    try Writer.init().write(foo1, file.outStream());
    try file.seekTo(0);
    try Writer.init().write(x, fbs.outStream());
    try fbs.seekTo(0);
    var r = Reader.init(allocator);
    defer r.deinit();
    const foo2 = try r.read(Foo, file.inStream());
    assert(std.meta.eql(foo1, foo2));
    const y = try r.read(Bar, fbs.inStream());
    warn("x: {}\ny: {}\n", .{ x, y });
}